C++ Solution using One Pass DFS with O(n) Time Complexity


  • 0
    Y
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* convertBST(TreeNode* root) {
            if(root==NULL)
                return root;
            int tot = 0;
            dfs(root, tot);
            return root;
        }
    private:
        void dfs(TreeNode *root, int &tot){
            if(root->right){
                dfs(root->right, tot);
            }
            tot += root->val;
            root->val += (tot - root->val);
            if(root->left){
                dfs(root->left, tot);
            }
        }
    };
    

Log in to reply
 

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