Java Recursive O(n)


  • 0
    V
    public class Solution {
        public TreeNode convertBST(TreeNode root) {
            convertHelper(root,0);
            return root;
        }
        
        public int convertHelper(TreeNode root,int sum)
        {
            if(root == null)
                return 0;
            int right = convertHelper(root.right,sum);
            root.val = root.right == null ? root.val + sum : root.val + right;
            int left = convertHelper(root.left,root.val);
            return root.left == null ? root.val : left;
        }
    }
    

Log in to reply
 

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