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. day04 JS

    很伤心,就在前天下午,本人的电脑突然挂了,电脑售后告知需要10个工作日才可修好. 于是乎,昨天学的内容来不及整理,暂且跳过,改天再抽空补上,就当缓几天再复习吧. 今天继续学习了JS的内容. 1 js的 ...

  2. 十二、VueJs 填坑日记之项目打包发布

    通过上一篇博文的学习,我们其实已经完成了我们设想的项目的开发.但是,我们做好的这套东西,是基于 nodejs 开发的.而我们最终希望,我们开发的项目,生成好一堆文件,然后随便通过任何一个 http 服 ...

  3. android动画基础之Animation

    android 动画 摘要: 概述 最近总结一下Android的一些东西,毕竟基础不牢地动山摇.本篇主要涉及Animation,对Tween和Frame动画做些总结. Tween Tween动画即补间 ...

  4. 在ThinkPHP中使用常量解决路由常规地址不安全传送数据问题

    在ThinkPHP搭建项目的同时,会考虑到后期对静态页面的维护问题, 在项目的不断完善的同时,会有大量图片,css文件,以及js文件等一些容易修改.添加.或者删除的资源 如果在中后期对各个静态页面,j ...

  5. codeforces 893D Credit Card 贪心 思维

    codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会 ...

  6. upload 上传类

    <?php/**file: fileupload.class.php 文件上传类FileUpload本类的实例对象用于处理上传文件,可以上传一个文件,也可同时处理多个文件上传 */class U ...

  7. PhpStorm2017版激活方法、汉化方法以及界面配置

    PhpStorm激活和汉化文件下载网址:http://pan.baidu.com/s/1nuHF1St(提取密码:62cg) PHPMailer的介绍 PhpStorm是一个轻量级且便捷的PHP ID ...

  8. C# Dictionary根据Key排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  9. Python 开发与接口测试学习笔记

    这是我跟着虫师学习中积累下来的学习笔记,写得比较简单,适合想学习Python开发与接口测试的初学者学习. 一.开发投票系统 1.参考官网文档,创建投票系统. https://docs.djangopr ...

  10. 关于C语言中static保留字的使用

             static存储类型可以用于全部变量,无需考虑变量声明的位置.但是作用于块外部和块内部时具有不同的作用.         (1)当作用于函数内部时,和每次程序离开所在块就会丢失值的自 ...