Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 3, 2015 13:33
Show Gist options
  • Select an option

  • Save rohith2506/544edceb78e8b60e8135 to your computer and use it in GitHub Desktop.

Select an option

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
// 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