C++ 13 lines Reverse Inorder traversal (beats 93%) (O(n) time O(1) space)


  • 0
    D
    class Solution {
        int sum1;
    public:
        Solution():sum1(0){}
        TreeNode* convertBST(TreeNode* root) {
            if(!root) return nullptr;
            TreeNode *right=convertBST(root->right);
            this->sum1+=root->val;
            root->val=this->sum1;
            TreeNode *left=convertBST(root->left);
            return root;
        }
    };
    

Log in to reply
 

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