C++ recursive O(n)


  • 0
    M
        int _convertBST(TreeNode* root, int & sum)
        {
            if(root == NULL)
                return 0;
            
            int r = _convertBST(root->right,sum);
            root->val += sum;
            sum = root->val;
            int l = _convertBST(root->left,sum);
            
            return l+r+root->val;
        }
        
        TreeNode* convertBST(TreeNode* root) {
            
            int sum = 0;
            _convertBST(root,sum);
            return root;
            
        }
    

Log in to reply
 

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