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

给定一个二叉搜索树,要求更改每个节点的值为树中其它比他大的节点值之和。二叉搜索树的特点是中序遍历的结果为一个从小到大的序列。

每个节点的值为其右侧所有节点的和,可以使用中序遍历的方法求解该问题,
class Solution {
int sum = 0; //全局变量
public TreeNode convertBST(TreeNode root) {
if(root == null) return null;
convertBST( root.right);//先右边
root.val += sum;
sum = root.val;
convertBST( root.left); return root;
}
}
 
 
 
 

538. 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. [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 解题报告

    题目要求 Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the origi ...

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

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

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

  8. 538 Convert BST to Greater Tree 把二叉搜索树转换为累加树

    给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和.例如:输入: 二叉搜索树:     ...

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

随机推荐

  1. [动态规划]P1220 关路灯

    题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉这些路灯. 为了给村 ...

  2. Ubuntu安装pyenv实现多版本控制

    Ubuntu安装pyenv实现多版本控制 git clone git://github.com/yyuu/pyenv.git ~/.pyenv echo 'export PYENV_ROOT=&quo ...

  3. JAVA基础3——常见关键字解读(1)

    常见的JAVA中的关键字 static static静态变量 静态变量:使用static关键字定义的变量.static可以修饰变量和方法,也有static静态代码块.被static修饰的成员变量和成员 ...

  4. OC面向对象的三大特性

    一.面向对象的三大特性:封装(成员变量).继承和多态 1. set方法和get方法 1. set方法和get方法的使用场合 @public的成员可以被随意赋值,应该使用set方法和get方法来管理成员 ...

  5. c语言的typedef

    一.typedef作用简介 1.作用:给已经存在的类型起一个新的名称 2.使用场合: 1> 基本数据类型 2> 指针 3> 结构体 4> 枚举 5> 指向函数的指针 * ...

  6. css一些简单的例子

    1.http协议 一:HTTP协议:hypertext transport protocol(超文本传输协议) 特点: 1.请求与响应 2.无状态的协议 请求协议: 请求首行: 请求头信息: Acce ...

  7. [flask实践] 解决qq邮箱/mysql的相关配置问题

    笔者经过flask web(Miguel著,封面是一条狗)一书的学习,打算实现一个旅游类网站,在此过程中发现,相对于书中的flasky博客程序,需要作出一些改变: 1. 注册邮箱:国内要使用126,q ...

  8. 对NumPy中dot()函数的理解

    今天学习到numpy基本的运算方法,遇到了一个让我比较难理解的问题.就是dot函数是如何对矩阵进行运算的. 一.dot()的使用 参考文档:https://docs.scipy.org/doc/num ...

  9. 51Nod 1352 集合计数 扩展欧几里得

    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足 ...

  10. CCF-201409-2-画图

    问题描述 试题编号: 201409-2 试题名称: 画图 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在一个定义了直角坐标系的纸上,画一个(x1,y1)到(x2,y2)的矩 ...