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. Oracle——基础知识(一)

       一.Oracle中的数据类型 1.字符串类型.如:char.nchar.varchar2.nvarchar2.2.数值类型.如:int.number(p,s).integer.smallint. ...

  2. scrapy xpath 节点关系

    父节点 子节点 兄弟节点 先辈节点 后代节点

  3. 字符串解压缩类库(zip、GZIP、QuickLz、snappy、lzf、jzlib)介绍

    1.ZIP. GZIP  计算机文件压缩算法,JDK中java.util.zip.*中实现.主要包括ZipInputStream/ ZipOutputStream.GZipInputStream/Zi ...

  4. OpenCV 官方工程报错(1) Couldn't load mixed_sample from loader

    openCV/OpenCV-android-sdk/samples/tutorial-2-mixedprocessing 工程 - ::): Trying to get library list - ...

  5. cookie禁用后的session

    在浏览器地址后加:jsessionid="对应的32位字符串",照样可以访问. 在用户角度来说,浏览器开启,关闭就是一次会话. 在服务器角度来说,session失效才代表一次会话的 ...

  6. sqlserver 查询int类型 in (字符串) 报转换int类型出错的问题

    , , '') ) AS c_departNames FROM t_user AS A LEFT JOIN t_role AS B ON A.c_roleId=B.c_roleId 用 CHARIND ...

  7. 读书笔记<深入理解JVM>01 关于OutOfMemoryError 堆空间的溢出

    代码片段如下: package com.gosaint.shiro; import java.util.ArrayList; import java.util.List; public class H ...

  8. CSS特性:white-space: nowrap;text-overflow: ellipsis;text-decoration: none

    /*white-space: nowrap; text-overflow: ellipsis; text-decoration: none;*/ /*这三句话必须连着使用, * clip: 当对象内文 ...

  9. 阿里云、宝塔、wordpress建站

    1 阿里云 购买一个学生机就行啦 2 宝塔 2.1 更改阿里云的镜像 技巧01:先关掉阿里云之前的镜像 技巧02:到镜像市场中寻找宝塔的镜像资源 2.2 配置安全组 宝塔的控制面板需要开通端口 888 ...

  10. 通过id设置的css属性和通过元素设置的css属性冲突了,优先级哪个高?

    ---恢复内容开始--- <!DOCTYPE html> <html> <head> <title>div test</title> < ...