[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 思路:中序遍历;然后关键点在于用了一个全局变量;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution{
private int sum = 0;
public TreeNode convertBST(TreeNode root) {
helper(root);
return root;
}
public void helper(TreeNode root){
if (root!=null){
helper(root.right);
root.val+=sum;
sum = root.val;
helper(root.left);
}
}
}
[Leetcode]538. 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 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 ...
随机推荐
- 命令行 app
[Getting Title at 38:53](http://click.pocoo.org/5/) 其实在 实际操作中 如果参数特别多的话,最好使用配置文件来操作,比如 yaml [YAML 语言 ...
- FCC学习笔记(二)
Nest an Anchor Element within a Paragraph 作为参考,再次看一看a元素的图示: 例如: <p>Here's a <a href="h ...
- 记一次Dynamic Batching不生效的爬坑实例分析[Unity]
最近在项目开发过程中,无意发现游戏场景的绘制占用了大量的Batches,几乎一个模型显示就占用了一个Batch,而Saved by batching数量几乎为0,即没有任何合批渲染优化.这显然跟预期相 ...
- SpringAop注解实现日志的存储
一.介绍 1.AOP的作用 在OOP中,正是这种分散在各处且与对象核心功能无关的代码(横切代码)的存在,使得模块复用难度增加.AOP则将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封 ...
- 基于jQuery的控件:弹框
★页面展示 ★属性 属性 值 说明 默认值 div Object jQuery对象 $('body') width Number 控件的宽度 auto height Number 控件的高度 auto ...
- f12 headers 变字典快捷方式
- JS加密对应的c#解码
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,? ...
- Java作业十三(2017-11-20)
/*使用一位数组解决 1 1 2 3 5 8 13 数列问题 斐波纳契数列 Fibonacci*/ package cn.GM; public class array { public static ...
- 回顾4180天在腾讯使用C#的历程,开启新的征途
今天是2018年8月8日,已经和腾讯解除劳动关系,我的公司正式开始运营,虽然还有很多事情需要理清,公司官网也没有做,接下来什么事情都需要自己去完成了,需要一步一个脚印去完善,开启一个新的征途,我将在博 ...
- kali linux 网络渗透测试学习笔记(二)OWASP ZAP工具扫描SQL injection漏洞失败
按照惯例,利用OWASP ZAP工具扫描SQL injection漏洞时,应该很快就可以扫描出来,但是在笔者进行扫描的时候,却遇到了以下状况: 这说明了该工具根本就没能够扫描出SQL注入的漏洞,不知道 ...