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) {
helper(root);
return root;
} public void helper (TreeNode root){
if(root == null){
return;
}
helper(root.right);
sum = sum + root.val;
root.val = sum;
helper(root.left); }
}

反着中序遍历, 就会从大到小排序,注意要用一个 global 变量保存状态。

LeetCode - Convert BST to Greater Tree的更多相关文章

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

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

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

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

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

  5. 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

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

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

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

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

随机推荐

  1. java串口编程

    报错:no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDrive java.lang.Unsatisfie ...

  2. oo作业总结报告2

    第五次作业 多线程电梯 多线程同步和控制的设计策略 明确类和对象,以及是否区分对象实例.具体类可以从类图中看出: 明确线程的类型和数量.输入作为一个线程,调度作为一个线程,三个电梯独立工作,互不影响, ...

  3. Hive/Hbase/Sqoop的安装教程

    Hive/Hbase/Sqoop的安装教程 HIVE INSTALL 1.下载安装包:https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-2.3 ...

  4. tp配置

    <?php// +----------------------------------------------------------------------// | ThinkPHP [ WE ...

  5. 使用MyEclipse开发Java EE应用:用XDoclet创建EJB 2 Session Bean项目(二)

    [MyEclipse最新版下载] 二.创建一个Session EJB – Part 1 MyEclipse中的EJB 2.x开发使用了EJB向导和集成XDoclet支持的组合. 每个EJB由三个基本部 ...

  6. 运行时常量池中的符号引用/String.intern() /ldc指令

    运行时常量池,之前放在方法区(永久代)中,1.8之后被转移到元空间,放到了native memory中. 具体的数据结构是:(看对象的内存布局,句柄访问还是对象头中保存指向类的元数据的指针,这里以对象 ...

  7. 51nod1042

    给出一段区间a-b,统计这个区间内0-9出现的次数.   比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次. Inp ...

  8. css3宽高设置:calc() / vw / vh

    对于720px的设计稿,100vw == 720px,1vw == 7.2px; vw可以替代rem 实现自适应布局. 相应的计算插件:postcss-px-to-viewport ******** ...

  9. Java 内存监控(一)之 jps命令

    今天看一下Java命令行工具 jps的使用 一.命令简介 jps [ options ] [ hostid ] 不输入 [ hostid ] 内容,则默认是本机. 二.options选项的内容 -q ...

  10. ubatu 安装nodejs npm liveserver

    更新ubuntu软件源 sudo apt-get update sudo apt-get install -y python-software-properties software-properti ...