One function O(n) 9 line JAVA Code


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

Log in to reply
 

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