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
Java Solution:
Runtime beats 89.45%
完成日期:07/07/2017
关键词:Tree
关键点:inorder 来遍历树; 每个点顺序是right, root, left
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
int sum = 0;
public TreeNode convertBST(TreeNode root)
{
if(root == null)
return null; convertBST(root.right); sum += root.val;
root.val = sum; convertBST(root.left); return root;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)的更多相关文章
- 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 ...
- 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 origi ...
- [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_easy】538. Convert BST to Greater Tree
problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_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 B ...
- LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
- 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
随机推荐
- php中的多条件查询
首先是查询所有,步骤不详述,连接数据库,查询表中的所有信息,foreach循环以表格的形式打印出来 然后就是form表单中提交查询的数据,这里以post方式提交到本页面,所以要判断post中是否有值, ...
- 在linux下通过hexdump生成一个十六进制的文本保存文件,解析此文件转变成正常源代码文件。
举例说明: 此十六进制保存的文件为此源代码hexdump生成的: #include<stdio.h> #include<string.h> #include<stdlib ...
- [04] 利用注解生成实体类对应的建表sql语句
1.实现功能 我们已经对注解有了基本的认识,知道了如何自定义注解,如何使用和最基本的处理注解. 本篇主要介绍,如何使用运行时级别的注解,配合反射来自动生成建表的sql语句.如下例: 我们有实体类Stu ...
- 在ZABBIX平台上通过SNMP协议监控网络设备
在ZABBIX平台上通过SNMP协议监控网络设备 方法一:自动发现监控项 ZABBIX自带模板Template SNMP Interfaces中有"自动发现规则"这一选项,在主机选 ...
- 用reduce实现阶乘计算
def fact(a,b): return a*b from functools import reduce print(reduce(fact,range(1,6))) from functools ...
- Eclipse将引用了第三方jar包的Java项目打包成jar文件
第一步:建议手动 Eclipse插件fatjar 安装方法:1:下载地址:http://downloads.sourceforge.net/fjep/net.sf.fjep.fatjar_0.0.27 ...
- oracle pl/sql 简介
一.pl/sql 是什么pl/sql(procedural language/sql)是oracle在标准的sql语言上的扩展.pl/sql不仅允许嵌入sql语言,还可以定义变量和常量,允许使用条件语 ...
- String的replace和replaceAll
replace(CharSequence target, CharSequence replacement) 这里CharSequence是一个接口 实现类包括CharBuffer, Segement ...
- java集合系列——Map之HashMap介绍(八)
1.HashMap的简介 (JDK1.7.0_79版本) HashMap是基于哈希表的Map实现的的,一个Key对应一个Value,允许使用null键和null值,不保证映射的顺序,特别是它不保证该顺 ...
- Json操作问题总结
大家都知道,Json是一种轻量级的数据交换格式,对JS处理数据来说是很理想滴! 熟练写过xxx.json文件和操作的小伙伴来说,我说的问题都不是什么大问题啦,可以忽略本宝宝的文章,更希望各位大佬指点一 ...