Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
Example:
Input: The root of a Binary Search Tree like this: 5 / 2 13 Output: The root of a Greater Tree like this: 18 / 20 13
1 class Solution { 2 int sum = 0; 3 public TreeNode convertBST(TreeNode root) { 4 convert(root); 5 return root; 6 } 7 public void convert(TreeNode root){ 8 if(root==null) return; 9 convert(root.right); 10 root.val+=sum; 11 sum = root.val; 12 convert(root.left); 13 } 14 }