Level:

  Easy

题目描述:

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) {
if(root==null)
return null;
if(root!=null){
convertBST(root.right);//先访问右子树
root.val=root.val+sum; //更新节点的值
sum=root.val;
convertBST(root.left);
}
return root;
}
}

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

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

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

  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 original B ...

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

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

  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. 2015.3.31不使用debug/X86文件夹方式解决64/32位问题

    传统方法:在解决方案-配置管理器-新建X86平台可以解决32位兼容问题,但是Debug目录下会多出X86文件夹.不方便 另一种方法:在项目名称-属性-生成-目标平台-x86也能解决问题,而且不出现X8 ...

  2. Drools学习笔记3—Conditions / LHS—字段约束连接&字段约束操作符

    字段约束连接 用于字段约束 对象内部多个约束连接,采用“&&”(and).“||”(or)和“,”(and) 执行顺序:“&&”(and).“||”(or)和“,” 字 ...

  3. composer update的错误使用以及如何更新composer.lock文件

    用composer update装包是错误的. 安装包标准的方法应该是 require ,或者手动写 compose.json 文件,然后 composer install .如果只是需要更新 com ...

  4. mysql应用基本操作语句(转)

    二.库操作 1..创建数据库 命令:create database <数据库名> 例如:建立一个名为xhkdb的数据库 mysql> create database xhkdb; 2 ...

  5. jsp与struts的区别

    JSP通常用于MVC的View层,Struts1,Struts2用于MVC的Control层. JSP用来展示页面信息,使用servlet API封装而成,代替servlet中response向客户端 ...

  6. 01-19asp.net基础--网站登录及验证

    第一步: 1)首先使用“CodeSmith”将Examinee类实体化,并生成实体类连接数据库的方法,存在解决方案下的“App_Code”文件夹下. 修改一下连接某个数据库: private SqlC ...

  7. tomcat urlwrite报错

    十二月 26, 2017 2:15:30 下午 org.apache.catalina.core.ApplicationContext logINFO: org.tuckey.web.filters. ...

  8. NDK 编译报错:request for member 'FindClass' in something not a structure or union

    ndk编译 xx.c文件时一直报下面的错误: ”request for member 'FindClass' in something not a structure or union ...” 原因 ...

  9. css垂直居中方法(一)

    第一种方法:首先用margin:0 auto实现水平居中,然后设置position:relative,设置top为50%(父元素高度的50%),然后设置margin-top:-150px(设置负值是因 ...

  10. Swing绘图API

    ----------------siwuxie095                             工程名:TestSwingPaintAPI 包名:com.siwuxie095.swing ...