leetcode 538. 把二叉搜索树转换为累加树

题目

C++代码

/**
 * 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)
            fun(root, 0);
        return root;
    }
    int fun(TreeNode* root, int sum)
    {
        if(root->right)
        {
            sum = fun(root->right, sum);
        }
        int val = root->val;
        root->val += sum;
        sum += val;
        if(root->left)
        {
             sum = fun(root->left, sum);
        }
        return sum;
    }
};
原文地址:https://www.cnblogs.com/xumaomao/p/11382099.html