Skip to content

Instantly share code, notes, and snippets.

@JulianAR97
Created June 8, 2021 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulianAR97/ea4b9af127687e553315d9ed19fb13fb to your computer and use it in GitHub Desktop.
Save JulianAR97/ea4b9af127687e553315d9ed19fb13fb to your computer and use it in GitHub Desktop.
Greater Than Tree Solution
let sum = 0 // sum will be added to all 'less than' nodes
const convertBST = (root) => {
if (root == null) return;
convertBST(root.right); // traverse right
sum += root.val; // add root value to 'global' sum
root.val = sum; // new value becomes sum of all values that have already been seen
convertBST(root.left) // traverse left
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment