Shared my c++ solution


  • 0
    class Solution {
    vector<int> vec;
    public:
        TreeNode* convertBST(TreeNode* root) {
            int sum = 0;
            int index = 0;
            Inordered(root,0,sum,index);
            Inordered(root,1,sum,index);
            return root;
     }    
    
        void Inordered(TreeNode *root,int flag,int &sum,int &index){
            if(!root)
                return;
            Inordered(root->left,flag,sum,index);
            if(flag==0){
                vec.push_back(root->val);
                sum += root->val;
            }
            else{
                root->val += (sum -= vec[index++]);
            }
            Inordered(root->right,flag,sum,index);
        }
    };
    

Log in to reply
 

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