C++, no global variable


  • 0
    Z
    class Solution {
    public:
        TreeNode* convertBST(TreeNode* root) {
            traverse(0, root);
            return root;
        }
    private:
        int traverse(int pre_val, TreeNode *root) {
            if(!root)   return 0;
            int val = root -> val, r_val = traverse(pre_val, root -> right);
            root -> val += pre_val + r_val;
            return traverse(root -> val, root -> left) + val + r_val;
        }
    };
    

Log in to reply
 

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