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

题目标题:Tree
  这道题目给了我们一个二叉搜索树,特性是 left < root < right 对于每一个点。基于这个特性,如果我们要把 每一个点的值 变成 所有比这个点大的sum 再加上它自己,这样的话,我们必须从最右边底下的点开始遍历,因为它是最大的点。设一个sum,利用inorder 来遍历tree, 每次当一个点,它的右边点返回之后,更新sum的值, 用sum = sum + 这个点的val。再把这个点的值更新 = sum。所以根据这个顺序,我们看一下原题给的例子,利用inorder 顺序,结合 right root left 顺序, 我们先一直走到最右边底的点,13, 13右边返回的null, 然后sum = 0 + 13, 13这个点的值就等于sum(13),再去left,这个left返回的也是null。 接着13返回到5这个点, 5的右边点是13, 那么sum = 13+5 (18) 5这个点就更新为18。再去left, 2这个点,2的右边返回的是null, 然后sum = 18 + 2 (20), 再去2的left,null。最后返回到root。
 
 

Java Solution:

Runtime beats 89.45%

完成日期:07/07/2017

关键词:Tree

关键点:inorder 来遍历树; 每个点顺序是right, root, left

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
int sum = 0;
public TreeNode convertBST(TreeNode root)
{
if(root == null)
return null; convertBST(root.right); sum += root.val;
root.val = sum; convertBST(root.left); return root;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)的更多相关文章

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

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

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

  3. 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), ...

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

  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. 【leetcode_easy】538. Convert BST to Greater Tree

    problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完

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

  8. LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)

    题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...

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

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

随机推荐

  1. Java:输入输出流 java.io包的层次结构

    1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...

  2. linux c函数指针的应用

    头文件:1.h #include<stdio.h> int nul_func(); int test1(int a,int b); int test2(int a,int b,int c) ...

  3. allego 输出报告说明

    List of Available Reports Assigned Function Report Lists all assigned functions, sorted by function ...

  4. codeforces 862B B. Mahmoud and Ehab and the bipartiteness

    http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且 ...

  5. 理解 IntelliJ IDEA 的项目配置和Web部署

    1.项目配置的理解 IDEA 中最重要的各种设置项,就是这个 Project Structre 了,关乎你的项目运行,缺胳膊少腿都不行.最近公司正好也是用之前自己比较熟悉的IDEA而不是Eclipse ...

  6. 微信小程序语音识别服务搭建全过程解析(项目开源在github)

    silk v3录音转olami语音识别和语义处理的api服务(ubuntu16.04服务器上实现) ## 重要的写在前面 重要事项一: 目前本文中提到的API已支持微信小程序录音文件格式:silk v ...

  7. 在Ubuntu上安装arm-linux-gcc的问题

    由于之前将Ubuntu的更新关掉了,所以导致我下载32位兼容包一直出错. 在arm-linux-gcc 安装之后,还不能编译程序的话,首先看自己的系统是多少位的,因为网上大部分的安装包都是32位的,所 ...

  8. Count Color 线段树

    Count Color Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. .Neter玩转Linux系列之六:Linux下MySQL的安装、配置、使用

    一.Linux安装MySQL (1)下载安装包:https://dev.mysql.com/downloads/mysql/ (2)解压并安装 命令:tar zxvf 文件名 解压完成之后,重名一下文 ...

  10. Ubuntu下的终端多标签切换快捷键

    ubuntu下由于常在终端下工作,也同样需要在一个终端窗口下开启多个标签方便日常开发工作(vim党,尽量避免使用鼠标) 方法一: alt+1 alt+2 alt+3 方法二: ctrl + pageU ...