C++ 3 lines


  • 0
    H
    class Solution {
    public:
        TreeNode* convertBST(TreeNode* root) {
            dfs(root, 0);
            return root;
        }
        
        int dfs(TreeNode* root, int sum) {
            if (root == NULL) { return sum; }
            root->val += dfs(root->right, sum);
            return dfs(root->left, root->val);
        }
    };
    

Log in to reply
 

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