Last active
May 26, 2018 23:30
-
-
Save thmain/3c45e7a02201f89b17e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BSTtoGreaterTree { | |
public static int sum = 0; | |
public void greaterTree(Node root){ | |
if(root!=null){ | |
//visit the right node first | |
greaterTree(root.right); | |
//store the node value in temp | |
int temp = root.data; | |
//update the sum, sum till previous visited node | |
root.data = sum; | |
//update the sum for the next node | |
sum = sum + temp; | |
greaterTree(root.left); | |
}else return; | |
} | |
public void inorder(Node root){ | |
if(root!=null){ | |
inorder(root.left); | |
System.out.print(" " + root.data); | |
inorder(root.right); | |
} | |
} | |
public static void main(String args[]){ | |
Node root = new Node(10); | |
root.left = new Node(5); | |
root.right = new Node(15); | |
root.left.left = new Node(2); | |
root.left.right = new Node(7); | |
root.right.left = new Node(12); | |
root.right.right = new Node(20 ); | |
BSTtoGreaterTree b = new BSTtoGreaterTree(); | |
b.inorder(root); | |
System.out.println(""); | |
b.greaterTree(root); | |
System.out.print("Greater Sum Tree: "); | |
b.inorder(root); | |
} | |
} | |
class Node{ | |
int data; | |
Node left; | |
Node right; | |
public Node (int data){ | |
this.data = data; | |
left = null; | |
right = null; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment