13.Convert BST to Greater Tree(将树转为更大树)
Level:
Easy
题目描述:
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 {
int sum=0;
public TreeNode convertBST(TreeNode root) {
if(root==null)
return null;
if(root!=null){
convertBST(root.right);//先访问右子树
root.val=root.val+sum; //更新节点的值
sum=root.val;
convertBST(root.left);
}
return root;
}
}
13.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 ...
- 538 Convert BST to Greater Tree 把二叉搜索树转换为累加树
给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(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 ...
- 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 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 ...
- [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 ...
随机推荐
- python连接sql server数据库
记录一下pyodbc连接数据库的使用方法和注意事项,基于python2.7: 前提: pip install pyodbc .下载pyodbc包. pyodbc.connect('DRIVER ...
- .Net 使用 GregorianCalendar 获取任意一天为一年的第几周
private static int GetWeekOfYear(DateTime dt) { GregorianCalendar gc = new GregorianCalendar(); int ...
- ArcEngine中多边形内外环的处理(转)
ArcEngine中多边形内外环的处理 原创 2012年09月06日 22:49:11 标签: object / null / 数据库 3462 Polylgon对象是由一个或多个Ring对象的有序集 ...
- JavaScript 书籍推荐(转)
作者:宋学彦链接:https://www.zhihu.com/question/19713563/answer/23068003来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Python02 标准输入输出、数据类型、变量、随记数的生成、turtle模块详解
1 标准输出 python3利用 print() 来实现标准输出 def print(self, *args, sep=' ', end='\n', file=None): # known speci ...
- Tensorflow 优化学习
# coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data pr ...
- 一堵墙IFC数据-wall.ifc
这是一面墙的IFC数据内容 =====================================文档内容======================================= ISO-1 ...
- C++标准库vector以及迭代器
今天看C++的书,出现了一个新的概念,容器vector以及容器迭代器. vector是同一种对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存. ...
- PCL—点云分割(最小割算法) 低层次点云处理
1.点云分割的精度 在之前的两个章节里介绍了基于采样一致的点云分割和基于临近搜索的点云分割算法.基于采样一致的点云分割算法显然是意识流的,它只能割出大概的点云(可能是杯子的一部分,但杯把儿肯定没分割出 ...
- WarTransportation TopCoder - 8404
传送门 分析 我们高兴的发现数据范围特别小,所以我们可以随便搞.因为一共只砍掉一条路,所以我们先算出对于任意一个点如果将它的出边割掉一条则它到达终点的最坏情况的最短距离是多少,然后我们从终点向起点反着 ...