[LeetCode] Convert BST to Greater Tree

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.

Example:

Input: The root of a Binary Search Tree like this:
              5
            /              2     13

Output: The root of a Greater Tree like this:
             18
            /             20     13

题目要求将一个BST转换成一个每个节点数值更大的数,要求在全树范围内将比本节点大的所有节点值相加到本节点。由二叉树的中序遍历可知,节点值为:(左) (中) (右)。转换后的树的节点值为:(左 + 中 + 右) (中 + 右) (右)。可以用一个sum值表示每次节点需要相加的值,利用递归传递这个sum。最后完成BST转换。核心是使用逆中序遍历进行遍历生成新的树。

class Solution {
public:
    TreeNode* convertBST(TreeNode* root) {
        int sum = 0;
        convertBSTcore(root, sum);
        return root;
    }
    void convertBSTcore(TreeNode* root, int& sum) {
        if (root == nullptr)
            return;
        convertBSTcore(root->right, sum);
        root->val += sum;
        sum = root->val;
        convertBSTcore(root->left, sum);
    }
};
// 36 ms

用迭代完成该过程。

class Solution {
public:
    TreeNode* convertBST(TreeNode* root) {
        if (root == nullptr)
            return 0;
        int sum = 0;
        stack<TreeNode*> s;
        TreeNode* node = root;
        while (node != nullptr || !s.empty()) {
            while (node != nullptr) {
                s.push(node);
                node = node->right;
            }
            node = s.top();
            s.pop();
            node->val += sum;
            sum = node->val;
            node = node->left;
        }
        return root;
    }
};
// 35 ms
时间: 2024-12-16 03:03:08

[LeetCode] Convert BST to Greater Tree的相关文章

[LeetCode] 538. Convert BST to Greater Tree

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. Example: Input: The root of a Binary Search Tree like thi

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

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. Example: Input: The root of a Binary Search Tree like thi

[LintCode] Convert BST to Greater Tree

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.Example Given a binary search Tree `{5,2,13}`: 5 / 2 13 Re

538. Convert BST to Greater Tree

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. Example: Input: The root of a Binary Search Tree like thi

13.Convert BST to Greater Tree(将树转为更大树)

Level: ??Easy 题目描述: 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. Example: Input: The root of a Binary

538 Convert BST to Greater Tree 把二叉搜索树转换为累加树

给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树:              5            /   \           2     13输出: 转换为累加树:             18            /   \          20     13详见:https://leetcode.com/problems/convert-

LeetCode算法题-Convert BST to Greater Tree(Java实现)

这是悦乐书的第255次更新,第268篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第122题(顺位题号是538).给定二进制搜索树(BST),将其转换为更大树,使原始BST的每个键都更改为原始键加上所有键的总和大于BST中的原始键.例如: 输入:二进制搜索树的根,如下所示: 5 / 2 13 输出:大树的根,如下所示: 18 / 20 13 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 0

把二叉搜索树转化成更大的树 &#183; Convert BST to Greater Tree

[抄题]: 给定二叉搜索树(BST),将其转换为更大的树,使原始BST上每个节点的值都更改为在原始树中大于等于该节点值的节点值之和(包括该节点). Given a binary search Tree `{5,2,13}`: 5 / 2 13 Return the root of new tree 18 / 20 13 [暴力解法]: 时间分析: 空间分析: [思维问题]: [一句话思路]: 反向求和并把和赋给root.val [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常

Convert BST to Greater Tree

这道题为简单题 题目: 思路: 1.我的思路:首先利用广搜,保存每个节点值在列表中,然后两个for循环列表,使对应列表中的每个值变为题目要求的值,然后再将广搜的列表与节点值列表对应. 这种方法复杂,时间空间复杂度高,运行时还出现超时 2.大神的思路:利用递归.其实我最开始也是想用递归,但是没有找到转换的规律,看了大神的解答才明白, 代码: 1.我的垃圾代码: 1 class Solution(object): 2 def convertBST(self, root): 3 ""&quo