C++ solution


  • 0
    G
    class Solution {
    public:
    
        int ConvertBstLarger(TreeNode *root, int Add)
        {
            if(root == NULL)
            {
                return (0);
            }
            
            int RightRes = ConvertBstLarger(root->right,Add);  
            int LeftRes = ConvertBstLarger(root->left,RightRes + Add + root->val);
            int Ret = root->val + LeftRes + RightRes;
            root->val += Add + RightRes;
            return Ret;
        }
    
        TreeNode* convertBST(TreeNode* root) 
        {
            ConvertBstLarger(root,0);
            return (root);
        }
    };

Log in to reply
 

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