Created
March 3, 2015 13:33
-
-
Save rohith2506/544edceb78e8b60e8135 to your computer and use it in GitHub Desktop.
Add all sum of nodes which are greater than current node in given BST
This file contains hidden or 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
| // Calculate whole sum | |
| //1.)Traverse the BST and calculate the total sum of all the nodes | |
| //2.) Again start "Inorder Traversing" and replace the first node with the calculated sum in step 1 | |
| //3.) Subtract that first node from the sum and move forward to the second node. | |
| //4.) Again keep on replacing and subtracting till you finish the inorder traversal | |
| //Complexity O(n+n) | |
| void sum_modify(t, tsum){ | |
| inorder(t->left, tsum); | |
| t->data = tsum; | |
| tsum = tsum - t->data; | |
| inorder(t->right, tsum); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment