[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 this:
              5
            /              2     13

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

题意:给定一个二叉搜索树,把它转换成为累加树,使得每个节点的值是原来的节点值加上所有大于它的节点值之和。做树的题一定要利用条件,就是特别的给了一个什么样的树,这个树有什么特性二叉搜索树的特点是 右>根>左,也就是说中序有序,我们把中序倒过来就正好是从大到小的什么意思呢,就是当你遍历到某个节点的时候,比它大的都已经遍历完了,我们只需维护一个max就行
class Solution {
    private int max = 0;
    private void convert(TreeNode root) {
        if (root == null)
            return;
        convert(root.right);
        root.val += max;
        max = root.val;
        convert(root.left);
    }
    public TreeNode convertBST(TreeNode root) {
        convert(root);
        return root;
    }
}
 

原文地址:https://www.cnblogs.com/Moriarty-cx/p/9784395.html

时间: 2024-11-05 06:10:04

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

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

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

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

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

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

把二叉搜索树转化成更大的树 · 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