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}`:

  1. 5
  2. / \
  3. 2 13

Return the root of new tree

  1. 18
  2. / \
  3. 20 13

  4. 逆中序遍历树,注意rootgreatSuminput greatSum和右子树sum的和;输入给左子树的greatSum root.val
  1. /**
  2. * Definition of TreeNode:
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left, right;
  6. * public TreeNode(int val) {
  7. * this.val = val;
  8. * this.left = this.right = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param root the root of binary tree
  15. * @return the new root
  16. */
  17. public TreeNode convertBST(TreeNode root) {
  18. // Write your code here
  19. if (root == null) {
  20. return root;
  21. }
  22. helper(root, 0);
  23. return root;
  24. }
  25. private int helper(TreeNode root, int greatSum){
  26. int sum = 0;
  27. if (root.right != null) {
  28. sum += helper(root.right, greatSum);
  29. }
  30. int temp = greatSum + sum;
  31. sum += root.val;
  32. root.val += temp;
  33. if (root.left != null) {
  34. sum += helper(root.left, root.val);
  35. }
  36. return sum;
  37. }
  38. }

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. tomcat是怎么找到项目lib目录下的jar包的,求大神解答

    是通过java代码动态的修改classpath吗,和classloader有关系吗

  2. 2014-2015 ACM-ICPC, Asia Xian Regional Contest GThe Problem to Slow Down You

    http://blog.csdn.net/u013368721/article/details/42100363  回文树 建立两棵回文树,然后count处理一遍就可以了,然后顺着这两棵树的边走下去就 ...

  3. JavaScript 声明提前机制

    声明提前机制 在JavaScript存在着这样一种预处理机制,即浏览器在解析JS代码时会将var声明的变量和function声明的函数提升到当前作用域的顶部.但是解析JS代码时对var和functio ...

  4. 记录在tiny6410平台上采用4GSD卡来启动uboot和烧写nand flash uboot

    下面这种方法是从网上转的 没有验证 环境:ubuntu 13.04一.首先制作sd启动盘: 插入SD卡    sudo dd iflag=dsync oflag=dsync if=tiny210v2- ...

  5. 使用java进行 AES 加密 解密?

    百度百科是这样定义的: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标 ...

  6. oracle 11.2 asynch descriptor resize等待事件

    asynch descriptor resize描述最近部分insert /*+ append */语句出现该等待时间,经查This event is set when the number of a ...

  7. [C++ Primer Plus] 第3章、处理数据(一)程序清单

    一.程序清单3.1(变量的一些知识点) #include<iostream> #include<climits> using namespace std; void main( ...

  8. 剑指offer(41)和为S的连续正数序列

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

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

  10. MAC之间共享屏幕

    A 机设置-->共享-->屏幕共享 ☑️勾选上,打开屏幕共享.   B 机 safari 里输入 vnc://ip 回车就可以共享屏幕了   转载请注明出处:https://www.cnb ...