Simple DFS, Global Variable, O(n) Time, O(1) Space Solution


  • 0
    int sum = 0;
        public TreeNode ConvertBST(TreeNode root)
        {
            ConvertBST_helper(root);
            return root;
        }
        void ConvertBST_helper(TreeNode root)
        {
            if (root == null)
                return;
    
            ConvertBST_helper(root.right);
            sum += root.val;
            root.val = sum;
            ConvertBST(root.left);
        }
    

Log in to reply
 

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