[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

Return the root of new tree

             18
            /             20     13

An in order traversal of a binary search tree gives an increasing sorted keys. For this problem, we need to visit all the keys in decreasing order. So we need to

use the in order traversal with a twist:  right subtree -> root -> left subtree.

Solution 1. Recursion

 1 public class Solution {
 2     private int sum = 0;
 3     public TreeNode convertBST(TreeNode root) {
 4         helper(root);
 5         return root;
 6     }
 7     private void helper(TreeNode node) {
 8         if(node == null) {
 9             return;
10         }
11         helper(node.right);
12         node.val += sum;
13         sum = node.val;
14         helper(node.left);
15     }
16 }


Solution 2. Iterative
 1 public class Solution {
 2     public TreeNode convertBST(TreeNode root) {
 3         Stack<TreeNode> stack = new Stack<TreeNode>();
 4         TreeNode curr = root;
 5         int sum = 0;
 6
 7         while(curr != null || !stack.isEmpty()) {
 8             while(curr != null) {
 9                 stack.push(curr);
10                 curr = curr.right;
11             }
12             curr = stack.pop();
13             curr.val += sum;
14             sum = curr.val;
15             curr = curr.left;
16         }
17         return root;
18     }
19 }
 
时间: 2025-01-01 20:05:11

[LintCode] 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

[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

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

[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

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-

把二叉搜索树转化成更大的树 &#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 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常

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

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