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), 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
递归 Accepted
二叉搜索树的特点是对于任一节点来说,其左子树结点都比其小,其右子树结点都比其大。所以对于本题,只需利用递归,先访问最右结点,以一个sum值记录从大到小的原数值和。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sum = 0;
TreeNode* convertBST(TreeNode* root) {
travel(root);
return root;
}
void travel(TreeNode* root) {
if (!root) return;
if (root->right) travel(root->right);
root->val = (sum += root->val);
if (root->left) travel(root->left);
}
};
LN : leetcode 538 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 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 ...
- 【leetcode_easy】538. Convert BST to Greater Tree
problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完
- 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【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 orig ...
- 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 把二叉搜索树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树: ...
- [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 ...
随机推荐
- BZOJ——2190: [SDOI2008]仪仗队
思路: 我们将其所在的位置设为(0,0),那么如果存在一个点(x,y),且有gcd(x,y)=k(k!=1),那么点(x/k,y/k)一定会将(x,y)挡住.而如果k=1,那么点(x,y)就一定会被看 ...
- Andorid使用WiFi 连接adb进行调试
无奈数据线连接常常掉线. 于是寻找wifi连接adb的方法,在github上搜索了一下client的源代码后编译后执行了下,发现能够行得通,于是记录一下. 相应的安卓client源代码在这wifi a ...
- 【Web API系列教程】1.1 — ASP.NET Web API入门
前言 HTTP不仅仅服务于web页面.同一时候也是构建暴露服务和数据的API的强大平台.HTTP有着简单.灵活和无处不在的特点.你能想到的差点儿全部平台都包括有一个HTTP库.所以HTTP服务能够遍及 ...
- Ckeditor通过Ajax更新数据
之前在表单中对ckeditor的赋值就直接是 $("#theadEditor").val(result); 而如今我想通过点击不同选项来使用Ajax在后台訪问数据.对ckedito ...
- poj 2728 最优比例生成树(01分数规划)模板
/* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1 ...
- 怎样把引用的jar包和本项目一起导出成jar文件
之所以要导出Runnable JAR.是由于我们希望将引用到的Jar包与本项目一起进行导出,所以不要选Jar file 选File/Export...然后Java/Runnable JAR file, ...
- iOS NSMutableDictionary中UIImage的存储和读取
思路:将UIImage转换成NSData,然后插入到NSMutableDictionary中.读取时,用NSData读出来,然后再转换成UIImage -存储 UIImage *image = [se ...
- react 引入 百度地图API
使用 Echarts 的地图的时候,发现报错,说 Bmap api is not loaded 百度地图API没有加载 乍一想,Echarts 用的也是 百度地图 啊,没有引入百度地图,还用个啥,当然 ...
- ACdream区域赛指导赛之手速赛系列(5) 题解
A - Problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submi ...
- Linux下Gcc 的编译过程
在linux下开发难免会用到gcc编译.GCC(GNU Compiler Collection.GNU编译器套装),是由 GNU 开发的编程语言编译器.它是GNU编译器套装以GPL许可证所发行的自由软 ...