当前位置:网站首页>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软件安装及基本操作(新建文件/导出)
- [numerical analysis exercise] Jacobi iteration method of third-order matrix
- For 3nm and below processes, ASML new generation EUV lithography machine exposure
- Are Transformers Effective for Time Series Forecasting?|填坑
- 为什么服务端程序都需要先 listen 一下
- 一种比读写锁更快的锁,还不赶紧认识一下
- First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
- 【StoneDB故障诊断】MDL锁等待
- Software testing interview question: what is the focus of unit testing, integration testing, and system testing?
- 8000 word explanation of OBSA principle and application practice
猜你喜欢

How can anyone ask how MySQL archives data?

How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath

Form of objects in memory & memory allocation mechanism

2022 2nd cyber edge cup cyber security competition Web

B站崩了,那晚负责修复的开发人员做了什么?

Station B collapsed. What did the developer responsible for the repair do that night?

光藕继电器

Read Plato farm's eplato and the reason for its high premium

一口气学完 Redis 集群方案

Project analysis (what is it training that can't be given)
随机推荐
【StoneDB故障诊断】系统资源瓶颈诊断
8000字讲透OBSA原理与应用实践
[question 23] Sudoku game with rotation | DFS (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
In depth understanding of recursive method calls (including instance maze problem, tower of Hanoi, monkey eating peach, fiboracci, factorial))
Lvs+kept highly available cluster
【OBS】P B 丢帧阈值 buffer_duration_usec
Pytoch distributed training
It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years
项目分析(从技术到项目、产品)
Software test interview question: when saving a text file under windows, a save dialog box will pop up. If a test case is established for the file name, how should equivalent classes be divided?
Cloud native microservices Chapter 3: haproxy+kept
光藕继电器
V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
基于简化的评分卡、Smote采样和随机森林的信贷违约预测
异常-Exception
2021-11-05 understand main method syntax, code block and final keyword
MySQL series - database tables, queries, sorting, and data processing functions
How to buy stocks on mobile phones? Is it safe to open an account
Regular expression exercise