当前位置:网站首页>Leetcode 669 pruning binary search tree -- recursive method and iterative method
Leetcode 669 pruning binary search 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/trim-a-binary-search-tree
The question :
Give you the root node of the binary search tree root , At the same time, the minimum boundary is given low And the maximum boundary high. By pruning the binary search tree , Make the values of all nodes in [low, high] in . Pruning trees Should not be Change the relative structure of the elements retained in the tree ( namely , If not removed , The original relationship between father and son should be preserved ). Can prove that , There is The only answer .
So the result should return the new root node of the pruned binary search tree . Be careful , The root node may change according to the given boundary .
Example 1:
Input :root = [1,0,2], low = 1, high = 2
Output :[1,null,2]
Example 2:
Input :root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output :[3,2,null,1]
Tips :
The number of nodes in the tree is in the range [1, 104] Inside
0 <= Node.val <= 104
The value of each node in the tree is only Of
Topic data input is an effective binary search tree
0 <= low <= high <= 104
Ideas :
Let's start with the recursive method :
Recursive method , You need to determine the parameters of the recursive method first 、 Return value . In fact, this problem involves the reconstruction of binary tree , So with the help of recursive method, return the form of node , It simplifies the code .
- When the current node traversed is less than the minimum value , Then the nodes in its left subtree are also less than the lowest value , We traverse directly to its right subtree .
- Empathy , When the current node traversed is greater than the maximum value , Then the nodes in its right subtree are also greater than the lowest value , We traverse directly to its left subtree .
- If the value of the current node is in the range , Then this node is reserved , Let's traverse its left and right subtrees to see if there is anything to refactor , Finally, return the current node to
Recursive method Java Code :
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
if (root == null) return null;
if (root.val < low) return trimBST(root.right, low, high);
if (root.val > high) return trimBST(root.left, low, high);
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
return root;
}
}
The following is the idea of iterative method :
First, we will root Adjust to nodes in the range , Then we'll talk about root Cut the nodes that are not in the range in the left and right subtrees of .
So how to cut ?
If it is traversing to the current node , Judge whether the current node is within the range , If it's not in range , Then discard the current node , In this way , You need to record the parent node of the current node ( In other topics, you may also need to record whether the current node is the left node or the right node of the parent node ), This will be more troublesome .
Then let's just Judge whether the child node of the current node needs to be cut , If the child node of the current node is not in the range , We directly use the son node of the son node to replace the son node of the current node ( I didn't expect it to be a little windy ). Do this , It is much more convenient in cutting operation !
Iterative method Java Code :
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
// First adjust root To the extent
while (root != null && (root.val < low || root.val > high)) {
if (root.val < low) root = root.right;
else root = root.left;
}
TreeNode cur = root;
// cut off root Nodes not in the range in the left subtree
while (cur != null) {
while (cur.left != null && cur.left.val < low) cur.left = cur.left.right;
cur = cur.left;
}
cur = root;
// cut off root Nodes not in the range in the right subtree
while (cur != null) {
while (cur.right != null && cur.right.val > high) cur.right = cur.right.left;
cur = cur.right;
}
return root;
}
}
边栏推荐
- University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
- Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
- 问题随记 —— 在 edge 上看视频会绿屏
- 简单配置PostFix服务器
- Golang unit test, mock test and benchmark test
- What is the difference between cloud server and cloud virtual machine
- Take you to API development by hand
- C language string practice
- 跨境电商:外贸企业做海外社媒营销的优势
- 互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
猜你喜欢

PS screen printing brush 131, many illustrators have followed suit
![How to read the source code [debug and observe the source code]](/img/40/a2fca67bcde3c468a739c6990325f4.jpg)
How to read the source code [debug and observe the source code]

vs2013已阻止安装程序,需安装IE10

Golang单元测试、Mock测试以及基准测试

Applet setting multi account debugging
![[RT thread] NXP rt10xx device driver framework -- Audio construction and use](/img/85/32a83eaa4b7f5b30d4d7c4f4c32791.png)
[RT thread] NXP rt10xx device driver framework -- Audio construction and use

Tensorboard quick start (pytoch uses tensorboard)

TensorBoard快速入门(Pytorch使用TensorBoard)

Play with fancy special effects. This AE super kit is for you

One brush 149 force deduction hot question-10 regular expression matching (H)
随机推荐
跨境电商:外贸企业做海外社媒营销的优势
Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
RDS数据库的监测页面在哪看?
Kotlin learning quick start (7) -- wonderful use of expansion
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
Golang单元测试、Mock测试以及基准测试
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
[combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
One brush 144 force deduction hot question-1 sum of two numbers (E)
Applet setting multi account debugging
Unity notes unityxr simple to use
1147_ Makefile learning_ Target files and dependent files in makefile
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
SWM32系列教程4-端口映射及串口应用
SVN如何查看修改的文件记录
IntelliJ 2021.3 short command line when running applications
SQL injection database operation foundation