当前位置:网站首页>Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
2022-07-03 17:27:00 【Hello, I'm Boger】
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/convert-bst-to-greater-tree
The question :
Give a binary Search for Root node of tree , The node values of the tree are different , Please convert it to an accumulation tree (Greater Sum Tree), Make each node node The new value of is equal to or greater than in the original tree node.val The sum of the values of .
As a reminder , The binary search tree satisfies the following constraints :
The left subtree of a node contains only the key Less than Node key node .
The right subtree of a node contains only the key Greater than Node key node .
Left and right subtrees must also be binary search trees .
Be careful : This topic and 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ identical
Example 1:
Input :[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output :[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Example 2:
Input :root = [0,null,1]
Output :[1,null,1]
Example 3:
Input :root = [1,0,2]
Output :[3,3,2]
Example 4:
Input :root = [3,2,4,1]
Output :[7,9,4,10]
Tips :
The number of nodes in the tree is between 0 and 104 Between .
The value of each node is between -104 and 104 Between .
All the values in the tree Different from each other .
The given tree is a binary search tree .
Ideas :
Code Capriccio recorded in 《 Binary tree : Conclusion !》 The summary written in it is very good , I'll post it here :
- It involves the construction of binary tree , Whether ordinary binary tree or binary search tree, there must be a preamble , All nodes are constructed first .
- Find the properties of ordinary binary tree , Generally, it is a post order , It is usually calculated by the return value of recursive function .
- Find the properties of binary search tree , It must be the middle order , Or you'll be blind to order .
Because this problem involves the evaluation of binary search tree attributes , So we use middle order traversal to do , But the middle order traversal of this problem is a special middle order traversal .
In fact, this problem is not difficult , As long as you understand the idea, it is very simple !
Mention binary search tree , Think immediately Using middle order traversal on binary search tree will get an ordered sequence , And what this topic says is accumulation ( Make each node node The new value of is equal to or greater than in the original tree node.val The sum of the values of .), In fact, it is to add the value of each node In this ordered sequence, the sum of all numbers after the corresponding value of the current node .
After understanding this idea , In fact, we traverse the binary search tree in reverse order ( That is, first traverse the right node , Then traverse the intermediate nodes , Finally, traverse the left node ), And this process uses a sum The current value can be superimposed by the variable .
We use iteration and recursion to do this problem respectively , Are to achieve the binary tree in the order of traversal ( Reverse order ).
Recursive method Java Code :
class Solution {
int sum;
private void search(TreeNode node) {
if (node == null) return;
search(node.right);
node.val += sum;
sum = node.val;
search(node.left);
}
public TreeNode convertBST(TreeNode root) {
search(root);
return root;
}
}
Iterative method Java Code :
class Solution {
public TreeNode convertBST(TreeNode root) {
TreeNode cur = root;
int sum = 0;
Deque<TreeNode> deque = new LinkedList<>();
while (cur != null || !deque.isEmpty()) {
if (cur != null) {
deque.push(cur);
cur = cur.right;
} else {
cur = deque.poll();
cur.val += sum;
sum = cur.val;
cur = cur.left;
}
}
return root;
}
}
边栏推荐
- 【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
- [combinatorics] recursive equation (the problem of solving recursive equation with multiple roots | the problem is raised)
- [combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
- Type conversion, variable
- Loop through JSON object list
- PR second time
- Notes on problems -- watching videos on edge will make the screen green
- Test your trained model
- 1164 Good in C
- Depth first search of graph
猜你喜欢

设计电商秒杀

Depth first search of graph

PHP online confusion encryption tutorial sharing + basically no solution

1147_ Makefile learning_ Target files and dependent files in makefile
![[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th](/img/f1/c96c4a6d34e1ae971f492f4aed5a93.jpg)
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th

Hongmeng third training

Kubernetes resource object introduction and common commands (4)

Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source

Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos

kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
随机推荐
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Detailed explanation of common network attacks
Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
[UE4] brush Arctic pack high quality Arctic terrain pack
1164 Good in C
【JokerのZYNQ7020】DDS_ Compiler。
Swm32 series Tutorial 4 port mapping and serial port application
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
AcWing 4489. 最长子序列
Vs2013 has blocked the installer, and ie10 needs to be installed
PHP online confusion encryption tutorial sharing + basically no solution
Rsync远程同步
PR second time
[combinatorics] recursive equation (case where the non-homogeneous part is exponential | example where the non-homogeneous part is exponential)
AcWing 3438. Number system conversion
POM in idea XML graying solution
Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
Golang单元测试、Mock测试以及基准测试
AcWing 3438. 数制转换
Web-ui automated testing - the most complete element positioning method