c++ easy solution


  • 0
    W
    class Solution {
        void tovector(TreeNode* root, vector<TreeNode*>& vec) {
            if (root && root->left) tovector(root->left, vec);
            if (root) vec.push_back(root);
            if (root && root->right) tovector(root->right, vec);
        }
        void add(vector<TreeNode*>& vec) {
            if (vec.size() < 2) return;
            for (int i=vec.size()-2; i>=0; --i) {
                vec[i]->val += vec[i+1]->val;
            }
        }
    public:
        TreeNode* convertBST(TreeNode* root) {
            vector<TreeNode*> vec;
            tovector(root,vec);
            add(vec);
            return root;
        }
    };
    

Log in to reply
 

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