Leetcode 538. Convert BST to Greater Tree

 

 要进行转这棵树,仅仅需要进行先遍历右子树,遍历中树,遍历左子树,然后加中间就ok

/**
 * 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:
    int sum = 0;
    TreeNode* convertBST(TreeNode* root) {
        if(!root) return NULL;
        convertBST(root->right);
        root->val = root->val + sum;
        sum = root->val;
        convertBST(root->left);
        return root;
    }
};

 

posted @ 2020-02-07 19:19  SteveYu  阅读(96)  评论(0编辑  收藏  举报