lintcode convert bst to greater tree

来源:互联网 发布:软件可靠性分析报告 编辑:程序博客网 时间:2024/06/13 21:15

1.Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST

2.英语不好的我一开始都看不懂啥意思...百度翻译啦啦啦

3./**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param root the root of binary tree
     * @return the new root
     */
     int s=0;
    TreeNode* convertBST(TreeNode* root) {
        // Write your code here
        if(root==NULL)return NULL;
        convertBST(root->right);
        root->val=s+root->val;
        s=root->val;
        convertBST(root->left);
        return root;
    }
};

4.一开始完全看不懂题.看了看同学的也完全不懂,感觉特别难,只能看看同学的复制上了......

原创粉丝点击