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. Unity之显示fps功能

    如下: using UnityEngine; using System.Collections; public class ShowFpsOnGUI : MonoBehaviour { public ...

  2. 宝岛探险,DFS&BFS

    问题描述: 小哼通过秘密方法得到一张不完整的钓鱼岛航拍地图.钓鱼岛由一个主岛和一些附属岛屿组成,小哼决定去钓鱼岛探险.下面这个10*10的二维矩阵就是钓鱼岛的航拍地图.图中数字表示海拔,0表示海洋,1 ...

  3. Linux中安装Python2.7

    原文地址:http://www.jianshu.com/p/6425d18d3e47   安装依赖的库 yum -y install python-devel openssl openssl-deve ...

  4. vue解决启动报错cjs loader.js Error: Cannot find module '../config'问题

    vue解决启动报错cjs loader.js Error: Cannot find module '../config'问题 今天下载了一个开源项目一直运行不了,折腾了半天才找到问题所在,config ...

  5. 史上最全python面试题详解(一)(附带详细答案(持续更新))

    1.简述解释型和编译型编程语言? 概念: 编译型语言:把做好的源程序全部编译成二进制代码的可运行程序.然后,可直接运行这个程序. 解释型语言:把做好的源程序翻译一句,然后执行一句,直至结束! 区别: ...

  6. 《CSS世界》读书笔记(四)--宽度分离

    <!-- <CSS世界>张鑫旭著 --> CSS流体布局下的宽度分离原则 所谓“宽度分离原则”,就是CSS中的width属性不与影响宽度的padding/border(有时候包 ...

  7. MVC设计思路

    MVC 学会重复.学会总结.学会预习和练习 前端页面  <---->   服务器(控制层.业务层.DAO层) <--->  DB 说明:无论是框架还是servletJSP,用的 ...

  8. Docker Kubernetes hostPort 代理转发

    Docker Kubernetes  hostPort 代理转发 hostPort: 1. 类似docker -p 映射宿主级端口到容器. 2. 容器所在的主机暴露端口转发到指定容器中. 3. hos ...

  9. iOS报错总结

    1. new Date('2018-07-18 14:58:32').getTime();      //在ios上拿报Invalid Date    无法转化 Safari 正确解析new Date ...

  10. Intellij IDEA 设置启动JVM参数

    目录 采用CMS垃圾回收配置: 采用G1垃圾回收配置: 参数说明: 通用参数: CMS机制才有的参数: G1机制才有的参数: 参考: 打开 IDEA 安装目录,看到有一个 bin 目录,其中有两个 v ...