Java Recursive O(n) time, 5 lines


  • 1
    class Solution {
        public TreeNode convertBST(TreeNode root) {
            foo(root, 0);
            return root;
        }
        
        public int foo(TreeNode node, int arg) {
            if(node == null) return arg;
            node.val += foo(node.right, arg);
            return foo(node.left, node.val);
        }
    }
    

Log in to reply
 

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