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. EmguCV3.0几个控件的使用

    值方图(Histogram)是一种统计图数据,在影像处理中最常被用来统计一张图像或是感兴趣(ROI)区域的色彩分布,在这边本人使用的EmguCV 2.4.0版的内建值方图工具只有被包含在WinForm ...

  2. js实现导航栏的吸顶操作

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  3. 数据库理论-范式(1NF、2NF、3NF)

    范式是“符合某一种级别的关系模式的集合,表示一个关系内部各属性之间的联系的合理化程度”. 第一范式(1NF)是指数据库表的每一列都是不可分割的基本数据项.(每个属性不可分割)第二范式(2NF)要求数据 ...

  4. [patl1-046]整除光棍

    解题关键:模拟除法 #include<cstdio> #include<cstring> #include<algorithm> #include<cstdl ...

  5. js面试题知识点全解(一闭包)

    闭包使用场景:1.函数作为返回值,如下场景 function F1(){ var a = 100 //自由变量 //返回一个函数(函数作为返回值) return function(){ console ...

  6. Tensorflow fetch和feed

    import tensorflow as tf #Fetch input1 = tf.constant(1.0)input2 = tf.constant(3.0)input3 = tf.constan ...

  7. 面试题:hibernate 第二天 快照 session oid 有用

    ## Hibernate第二天 ## ### 回顾与反馈 ### Hibernate第一天 1)一种思想 : ORM OM(数据库表与实体类之间的映射) RM 2)一个项目 : CRM 客户关系管理系 ...

  8. Python Matplotlib.plot Update image Questions

    1. 最近在测试一款设备,采集了一些设备后需要一帧一帧显示图像,经常使用Python,所以选用了Matplotlib进行图像操作 数据结构: timesatamp polar_distance hor ...

  9. vray学习笔记(3)-多维子材质是个什么东西

    多维子材质是个什么东西?为什么出现这个概念? 在3dsmax官方网站,我们可以看到它的定义: The Multi/Sub-Object material lets you assign differe ...

  10. 100198H Royal Federation

    传送门 题目大意 国家有N个城市,任意城市可以到达任意城市,是一棵树.国王要给这些城市分省份.每个省份最少M个城市,最多3M个城市.每个省有一个首府,首府不一定是这个省的城市,只是首府到这个省各个城市 ...