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.
Given a binary search Tree `{5,2,3}`:
5
/ \
2 13
Return the root of new tree
18
/ \
20 13
逆中序遍历树,注意root的greatSum是input greatSum和右子树sum的和;输入给左子树的greatSum 是root.val
- /**
- * Definition of TreeNode:
- * public class TreeNode {
- * public int val;
- * public TreeNode left, right;
- * public TreeNode(int val) {
- * this.val = val;
- * this.left = this.right = null;
- * }
- * }
- */
- public class Solution {
- /**
- * @param root the root of binary tree
- * @return the new root
- */
- public TreeNode convertBST(TreeNode root) {
- // Write your code here
- if (root == null) {
- return root;
- }
- helper(root, 0);
- return root;
- }
- private int helper(TreeNode root, int greatSum){
- int sum = 0;
- if (root.right != null) {
- sum += helper(root.right, greatSum);
- }
- int temp = greatSum + sum;
- sum += root.val;
- root.val += temp;
- if (root.left != null) {
- sum += helper(root.left, root.val);
- }
- return sum;
- }
- }
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; 完
- 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 ...
- [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 ...
- 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&Python] Problem 860. 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 ...
随机推荐
- tomcat是怎么找到项目lib目录下的jar包的,求大神解答
是通过java代码动态的修改classpath吗,和classloader有关系吗
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest GThe Problem to Slow Down You
http://blog.csdn.net/u013368721/article/details/42100363 回文树 建立两棵回文树,然后count处理一遍就可以了,然后顺着这两棵树的边走下去就 ...
- JavaScript 声明提前机制
声明提前机制 在JavaScript存在着这样一种预处理机制,即浏览器在解析JS代码时会将var声明的变量和function声明的函数提升到当前作用域的顶部.但是解析JS代码时对var和functio ...
- 记录在tiny6410平台上采用4GSD卡来启动uboot和烧写nand flash uboot
下面这种方法是从网上转的 没有验证 环境:ubuntu 13.04一.首先制作sd启动盘: 插入SD卡 sudo dd iflag=dsync oflag=dsync if=tiny210v2- ...
- 使用java进行 AES 加密 解密?
百度百科是这样定义的: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标 ...
- oracle 11.2 asynch descriptor resize等待事件
asynch descriptor resize描述最近部分insert /*+ append */语句出现该等待时间,经查This event is set when the number of a ...
- [C++ Primer Plus] 第3章、处理数据(一)程序清单
一.程序清单3.1(变量的一些知识点) #include<iostream> #include<climits> using namespace std; void main( ...
- 剑指offer(41)和为S的连续正数序列
题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- Ubuntu下 fatal error: Python.h: No such file or directory 解决方法
参考: fatal error: Python.h: No such file or directory Ubuntu下 fatal error: Python.h: No such file or ...
- MAC之间共享屏幕
A 机设置-->共享-->屏幕共享 ☑️勾选上,打开屏幕共享. B 机 safari 里输入 vnc://ip 回车就可以共享屏幕了 转载请注明出处:https://www.cnb ...