leetcode538. Convert BST to Greater Tree
https://www.cnblogs.com/grandyang/p/6591526.html
这个题本质上是中序遍历的反向。中序遍历是从小到大,而这个题目是从大到小,然后每个数加上比自己大的所有数的和。
因为实际上并没有改变树的结构,只是改变了原来树中节点的值,所以用void做返回值就可以了。
每次加sum实际上就是加的所有比当前节点大的数的和,并且这个和一定等于前一个节点的更新值。
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
int sum = ;
convertCore(root,sum);
return root;
}
void convertCore(TreeNode* root,int &sum){
if(root == NULL)
return;
convertCore(root->right,sum);
root->val += sum;
sum = root->val;
convertCore(root->left,sum);
}
};
leetcode538. Convert BST to Greater Tree的更多相关文章
- LN : leetcode 538 Convert BST to Greater Tree
lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...
- 【leetcode_easy】538. Convert BST to Greater Tree
problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完
- [Swift]LeetCode538. 把二叉搜索树转换为累加树 | Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- 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 B ...
- 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 B ...
- [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 B ...
- [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 B ...
- 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 origi ...
- Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
随机推荐
- webpack4 系列教程(一): 打包JS
webpack 本身就是为了打包js所设计,作为第一节,介绍怎么打包js. 1. 检验webpack规范支持 webpack支持es6, CommonJS, AMD. 创建vendor文件夹,其中mi ...
- 【22】访问者模式(Visitor Pattern)
一.引言 在这篇博文中,我将为大家分享我对访问者模式的理解. 二.访问者模式介绍 2.1 访问者模式的定义 访问者模式是封装一些施加于某种数据结构之上的操作.一旦这些操作需要修改的话,接受这个操作的数 ...
- HDU4278
Faulty Odometer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Python全栈学习_day011作业
1,写函数,传入n个数,返回字典{‘max’:最大值,’min’:最小值}例如:min_max(2,5,7,8,4) 返回:{‘max’:8,’min’:2}(此题用到max(),min()内置函数) ...
- Python 练习: 简单的用户登录判断
_user = "klvchen" _passwd = " counter = 0 while counter < 3: username = raw_input( ...
- 用JS来实现的第一个简单游戏 :贪吃蛇
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- instanceof和typeof的细节
我骑着小毛驴,喝着大红牛哇,哩个啷格里格朗,别问我为什么这木开心,如果活着不是为了浪荡那将毫无意义 今天来捋一捋我们平日经常用的instanceof和typeof的一些小问题 typeof: type ...
- 关于input的焦点事件
关于input的焦点事件 $(".scanf_integral").focus(function(){//获取焦点//获取焦点后触发的事件 }) $(".scanf_in ...
- C# 6 元组应用 Part 1:方便的字典工厂方法
首先是简单的实现: public static class CollectionExtensions { public static IDictionary<TKey, TValue> M ...
- Power BI 与 Azure Analysis Services 的数据关联:3、还原备份文件到Azure Analysis Services
Power BI 与 Azure Analysis Services 的数据关联:3.还原备份文件到Azure Analysis Services 配置存储设置 备份前,需要为服务器配置存储设置. ...