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

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的更多相关文章

  1. 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), ...

  2. 【leetcode_easy】538. Convert BST to Greater Tree

    problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 数据库SQL的分组函数

    分组函数:(五个) 1···max(expr):求expr的最大值 }\ 2···min(expr):求expr的最小值 }-- 数据类型是有规定的 3···sum(expr):求expr的总和 }- ...

  2. Alpha阶段敏捷冲刺日志的集合贴

    敏捷冲刺日志的集合贴 Day1(4.17):http://www.cnblogs.com/software-teamwork/p/8861426.html Day2(4.20):http://www. ...

  3. Python之多进程&异步并行

    由于python的gil,多线程不是cpu密集型最好的选择 多进程可以完全独立的进程环境中运行程序,可以充分的利用多处理器 但是进程本身的隔离带来的数据不共享也是一个问题,而且线程比进程轻量 impo ...

  4. Chrome保存整个网页为图片(终极解决方案!)

    打开需要保存为图片的网页 然后按F12,接着按Ctrl+Shift+P 在红框内输入full 点击下面的“Capture full size screenshot”就可以保存整个网页为图片了

  5. MATLAB raw格式转为bmp格式

    今天是第一天写博客,哈哈哈!把完成的数字图像作业放上来和大家一起分享一下! 如果有什么问题,希望大家和我多多交流 1518234852@qq.com width=512; height=512; im ...

  6. 分类统计的controller和service

    SpringMVC框架下的 部分代码: Controller控制器: @Resource ReviewTitleService reviewTitleService;//调用ReviewTitleSe ...

  7. Mac配置Hadoop最详细过程

    Mac配置Hadoop最详细过程 原文链接: http://www.cnblogs.com/blog5277/p/8565575.html 原文作者: 博客园-曲高终和寡 https://www.cn ...

  8. css技巧小计

    今天又学到两招,一招是把大框写相对定位,然后小框写绝对定位,运用top和left,想去哪里去哪里 另一招是边框渐变色,微信小程序,边框写渐变我没成功,然后大佬支招写一个大框,相对定位,然后设背景渐变色 ...

  9. 拼接字符串,生成tree格式的JSON数组

    之前做的执法文书的工作,现在需要从C#版本移植到网页版,从Thrift接口获取数据,加载到对应的控件中 之前用的easyui的Tree插件,通过<ul><li><span ...

  10. Cocos Creator 使用protobufjs

    Win7 + Creator 2.0.0 + protobufjs 6.8.8 1.下载安装protobufjs npm install -g protobufjs 可以看到protobufjs安装在 ...