当前位置:网站首页>Leetcode-538- convert binary search tree to cumulative tree
Leetcode-538- convert binary search tree to cumulative tree
2022-07-27 22:11:00 【benym】
# LeetCode-538- Convert binary search tree to accumulation tree
Given a binary search tree (Binary Search Tree), Turn it into an accumulation tree (Greater Tree), So that the value of each node is the sum of the original node value plus all nodes greater than it .
Example 1:
Input : Original binary search tree :
5
/ \
2 13
Output : Convert to an accumulation tree :
18
/ \
20 13# Their thinking
Method 1、 recursive :
Binary search tree is , When the root node in the tree is not empty , The value of all nodes on its left subtree is less than that of its root node . If its right subtree is not empty , Then the value of all nodes on the right subtree is greater than the value of its root node .
According to this characteristic , The cumulative tree can be obtained by traversing in reverse middle order , That is, traverse the right root and the left root first , At the same time, the node values are accumulated , Meet the definition when the value of a node is accumulated by all node values greater than it .
When root Isn't empty , Right subtree recursion , And accumulate node values , Then left subtree recursion , Finally back to root node
Method 2、 iteration :
Use one Stack To store nodes , Use the middle order traversal of reverse order to accumulate node values , The implementation method is similar to the iterative method of middle order traversal .
# Java Code 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int sum = 0;
public TreeNode convertBST(TreeNode root) {
if(root!=null){
convertBST(root.right);
sum+= root.val;
root.val = sum;
convertBST(root.left);
}
return root;
}
}# Java Code 2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode convertBST(TreeNode root) {
int sum = 0;
Stack<TreeNode> stack = new Stack<>();
TreeNode temp = root;
while(temp!=null||!stack.isEmpty()){
while(temp!=null){
stack.push(temp);
temp = temp.right;
}
temp = stack.pop();
sum +=temp.val;
temp.val = sum;
temp = temp.left;
}
return root;
}
}边栏推荐
- An2021 software installation and basic operation (new file / export)
- Pythia: Facebook's latest open source visual and language multitasking learning framework
- day 1 - day 4
- Internal class (detailed explanation of four internal classes)
- STM32项目分享---MQTT智能门禁系统(含APP控制)
- Enumeration and annotation
- MySQL data recovery process is based on binlog redolog undo
- LVS+Keepalived高可用群集
- V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
- Learn the use principle and core idea of thread pool from the source code
猜你喜欢

8000 word explanation of OBSA principle and application practice

It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years

day 1 - day 4

零钱通项目(两个版本)含思路详解
![[question 23] Sudoku game with rotation | DFS (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)](/img/75/c207f4f562fd5b547c5b3134113154.jpg)
[question 23] Sudoku game with rotation | DFS (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)

JVM garbage collection garbage collector and common combination parameters

8000字讲透OBSA原理与应用实践

只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)

STM32项目分享---MQTT智能门禁系统(含APP控制)

项目分析(从技术到项目、产品)
随机推荐
Small change project (two versions) with detailed ideas
B站崩了,那晚负责修复的开发人员做了什么?
Form of objects in memory & memory allocation mechanism
软件测试的就业前景到底怎么样?
Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
Interview question: what are the functions of fail safe mechanism and fail fast mechanism
深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))
LVS+Keepalived高可用群集
cocos:ccpDistance函数的简单运用以及实现眼球随着手指在眼眶中转动的功能
Simple use of enum
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?
An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
Live broadcast software app development, uniapp scroll view hidden scroll bar
成员方法及其传参机制
Go language learning notes - mutex start go language from scratch
Is log4j vulnerability still widespread?
Enumeration and annotation
It seems to be a bug of thread pool, but I think the source code design is unreasonable.
vs2019 release模式调试:此表达式有副作用,将不予计算。