Created
June 8, 2021 02:23
-
-
Save JulianAR97/ea4b9af127687e553315d9ed19fb13fb to your computer and use it in GitHub Desktop.
Greater Than Tree Solution
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
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