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. js中有哪几种函数?

    匿名函数,回调函数,递归函数,构造函数

  2. loj6277

    题解: 树状数组模板提 代码: #include<bits/stdc++.h> using namespace std; ; int num[N],n,a[N],l,r,c,opt; vo ...

  3. OO Summary Ⅳ

    测试与正确性论证的效果差异 测试,或者说用断言进行黑箱测试,用大量的数据进行“覆盖性测试”,目的是当分支覆盖率达到100%也就是理论上来说所有可能的输入都已经测试过了,而输出结果均是正确的,那么我们理 ...

  4. flask不定参数的传递。多参数,多次传递

    有的时候有一个分类查询,再来一个排序,这就有两个参数要传递多次. 还是不定长度,不定内容的传递. 这个是用request.args来实现: def home(): requests=request.a ...

  5. 每天CSS学习之text-align

    text-align是CSS的一个属性,其作用是设置文本的对齐方式.其值如下所示: 1.left:文本左对齐.如下所示: div{ text-align:left; } 结果: 2.right:文本右 ...

  6. hdu 1057 A + B Again

    A + B Again Problem Description There must be many A + B problems in our HDOJ , now a new one is com ...

  7. 6.5C++查找字符串

    参考:http://www.weixueyuan.net/view/6394.html 总结: find函数可以在字符串中查找子字符串中出现的位置.该函数有两个参数,第一个参数是待查找的子字符串,第二 ...

  8. 关于vivado implement后clock interaction报告的理解(更新中)

    对于较大工程很难避免遇到CDC问题,vivado自带的分析工具可以报告跨时钟状态. 详情参看手册UG906-Design Analysis and Closure Techniques. (1)关于p ...

  9. Centos7部署kubernetes准备工作(一)

    一.准备工作: 1.创建三台虚拟机:(在node1配置好环境,然后关机克隆出node2.node3.并修改网卡.主机名即可) linux-node1.example.com 192.168.43.21 ...

  10. java中String的equals()和 ==

    String a=new String("java"); String b=new String("java"); System.out.println(a.e ...