Convert BST to Greater Tree
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.
Given a binary search Tree `{5,2,3}`:
5
/ \
2 13
Return the root of new tree
18
/ \
20 13
逆中序遍历树,注意root的greatSum是input greatSum和右子树sum的和;输入给左子树的greatSum 是root.val
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root the root of binary tree
* @return the new root
*/
public TreeNode convertBST(TreeNode root) {
// Write your code here
if (root == null) {
return root;
}
helper(root, 0);
return root;
}
private int helper(TreeNode root, int greatSum){
int sum = 0;
if (root.right != null) {
sum += helper(root.right, greatSum);
}
int temp = greatSum + sum;
sum += root.val;
root.val += temp;
if (root.left != null) {
sum += helper(root.left, root.val);
}
return sum;
}
}
Convert BST to Greater Tree的更多相关文章
- 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), ...
- 【leetcode_easy】538. Convert BST to Greater Tree
problem 538. Convert BST to Greater Tree 参考 1. Leetcode_easy_538. Convert BST to Greater Tree; 完
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- [LeetCode&Python] Problem 860. 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 ...
随机推荐
- Vue学习1:实例及生命周期
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- session删除
<?php session_start(); //删除session $_SESSION=array(); //删除客户端cookie中存储的sessionID值 if(isset($_COOK ...
- flask_mail发送163邮件,报553错误的原因
最近在练习用flask_mail发送163邮件时报错: reply: '553 authentication is required,163 smtp9,DcCowAD3eEQZ561caRiaBA- ...
- 艾妮记账本微信小程序开发(失败版)
这是一个寒假假期作业,要求是用web开发或者微信小程序或者手机app开发的,我本来是打算用微信小程序开发的,但由于这个后台数据库连接需要通过https认证后的浏览器再访问MySQL.所以做到后台数据库 ...
- <转>jmeter(二十二)内存溢出原因及解决方法
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- 51nod1432贪心
n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? Input 第一行包含 ...
- 2019/4/8 wen text
构造器产生对象的步骤:1.为对象在内存中申请内存空间. 2.对对象的属性申请内存空间. 3.为属性进行初始化. 4.执行构造器中编写的其他代码. 静态方法调用: 类名.方法 非静态方法调用: ...
- oracle insert、append、parallel、随后查询的redo与磁盘读写
SQL> set autotrace traceonly statistics; SQL> insert into big_table_dir_test1 select * from bi ...
- JS设计模式(2)策略模式
什么是策略模式? 定义:根据不同参数可以命中不同的策略 主要解决:在有多种算法相似的情况下,使用 if...else 所带来的复杂和难以维护. 何时使用:有许多种情况,而区分它们的只是他们直接的行为. ...
- CSS【03】:CSS 基础选择器与三种引入方式
基础选择器 选择器:css 选择 html 标签的一个工具,是将 css 与 html 建立起联系,那么 css 就可以控制 html 样式 选择器其实就是给 html 标签起名字 标签选择器 作用: ...