[LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
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
这道题让我们将二叉搜索树转为较大树,通过题目汇总的例子可以明白,是把每个结点值加上所有比它大的结点值总和当作新的结点值。仔细观察题目中的例子可以发现,2变成了20,而20是所有结点之和,因为2是最小结点值,要加上其他所有结点值,所以肯定就是所有结点值之和。5变成了18,是通过20减去2得来的,而13还是13,是由20减去7得来的,而7是2和5之和。我开始想的方法是先求出所有结点值之和,然后开始中序遍历数组,同时用变量sum来记录累加和,根据上面分析的规律来更新所有的数组。但是通过看论坛,发现还有更巧妙的方法,不用先求出的所有的结点值之和,而是巧妙的将中序遍历左根右的顺序逆过来,变成右根左的顺序,这样就可以反向计算累加和sum,同时更新结点值,叼的不行,参见代码如下:
解法一:
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
int sum = ;
helper(root, sum);
return root;
}
void helper(TreeNode*& node, int& sum) {
if (!node) return;
helper(node->right, sum);
node->val += sum;
sum = node->val;
helper(node->left, sum);
}
};
下面这种方法写的更加简洁一些,没有写其他递归函数,而是把自身写成了可以递归调用的函数,参见代码如下:
解法二:
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
if (!root) return NULL;
convertBST(root->right);
root->val += sum;
sum = root->val;
convertBST(root->left);
return root;
}
private:
int sum = ;
};
下面这种解法是迭代的写法,因为中序遍历有递归和迭代两种写法,逆中序遍历同样也可以写成迭代的形式,参加代码如下:
解法三:
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
if (!root) return NULL;
int sum = ;
stack<TreeNode*> st;
TreeNode *p = root;
while (p || !st.empty()) {
while (p) {
st.push(p);
p = p->right;
}
p = st.top(); st.pop();
p->val += sum;
sum = p->val;
p = p->left;
}
return root;
}
};
参考资料:
https://leetcode.com/problems/convert-bst-to-greater-tree/
https://discuss.leetcode.com/topic/83455/java-recursive-o-n-time/2
https://discuss.leetcode.com/topic/83513/one-traverse-java-solution
https://discuss.leetcode.com/topic/83458/java-solution-7-liner-reversed-traversal
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树的更多相关文章
- 538 Convert BST to Greater Tree 把二叉搜索树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树: ...
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- LeetCode初级算法--树02:验证二叉搜索树
LeetCode初级算法--树02:验证二叉搜索树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
- 萌新笔记之二叉搜索树(BST)
前言,以前搞过线段树,二叉树觉得也就那样= =.然后数据结构的课也没怎么听过,然后下周期中考... 本来以为今天英语考完可以好好搞ACM了,然后这个数据结构期中考感觉会丢人,还是好好学习一波. 二叉搜 ...
随机推荐
- KVM之二:配置网络
1.安装KVM a.通过yum安装虚拟化的软件包 [root@kvm ~ ::]#yum install -y kvm virt-* libvirt bridge-utils qemu-img 说明: ...
- Android Intent 基本使用及对象构成
Intent基本使用 Intent可以理解为不同组件通信的媒介或者信使. Intent可以启动一个Activity,也可以启动一个Service,还可以发起一个广播Broadcast. 具体方法如下表 ...
- 网站加速与Linux服务器防护
网站加速方面 1. Nginx 配置 gzip 压缩 开启nginx gzip压缩后,网页.css.js等静态资源的大小会大大的减少,从而可以节约大量的带宽,提高传输效率,给用户快的体验.虽然会消耗c ...
- C#编程语言之委托与事件(二)—— C#事件
前面已经大致讲述了C#委托的一些基础知识点,本文接下来的内容是C#中的事件(Event),在此我提个建议,如果是刚接触C#的委托类型的朋友可以先看到这里,等熟悉了委托的使用之后(大约1-2天)再来了解 ...
- 关于python中的operator.itemgetter()函数的用法
1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2 ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.11.14
第一次会议:2017-11-14 额--这几天比较忙,忘记上传了,今天补上 先上个图,O(∩_∩)O哈哈: 会议主要内容: 1. 讨论整体框架 2. 个人具体分工 3. 代码统一 具体分工: 成员 计 ...
- sublime安装 和 插件安装
先从官网下载sublime https://www.sublimetext.com/3 安装完毕后 快捷键ctrl+` 或者View->Show Console,输入如下代码(sublime ...
- Python内置函数(44)——len
英文文档: len(s) Return the length (the number of items) of an object. The argument may be a sequence (s ...
- Python内置函数(28)——iter
英文文档: iter(object[, sentinel]) Return an iterator object. The first argument is interpreted very dif ...
- Python内置函数(17)——chr
英文文档: chr(i) Return the string representing a character whose Unicode code point is the integer i. F ...