Simple Java solution, sum up with inorder traversal


  • 0
    D
    public class Solution {
        public TreeNode convertBST(TreeNode root) {
            int sum = 0;
            sumInOrder(root,sum);
            return root;
        }
        private int sumInOrder(TreeNode root,int sum){
            if(root==null){
                return sum;
            }
            root.val += sumInOrder(root.right,sum);
            return sumInOrder(root.left,root.val);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.