当前位置:网站首页>669. Prune binary search tree ●●
669. Prune binary search tree ●●
2022-07-05 04:50:00 【chenyfan_】
669. Prune the binary search tree ●●
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 .
Input :root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output :[3,2,null,1]
For binary search trees ,
If root->val < low, Then the pruned binary tree must appear on the right of the root node ;
If root->val > high, Then the pruned binary tree must appear on the left of the root node .
recursive
Time complexity :O(N), among N Are all the nodes of a given tree . We can visit each node at most once .
Spatial complexity :O(N), Even if we don't explicitly use any additional memory , In the worst case , The stack of our recursive calls may be as large as the number of nodes .
- Traverse from top to bottom , After pruning a node , You need to judge this node again ( Return to the pruned subtree );
class Solution {
public:
TreeNode* pre = nullptr;
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root == nullptr) return nullptr;
if(root->val < low){
return trimBST(root->right, low, high); // Return to the pruned right subtree
}else if(root->val > high){
return trimBST(root->left, low, high); // Return to the pruned left subtree
}
root->left = trimBST(root->left, low, high); // Prune the left subtree
root->right = trimBST(root->right, low, high); // Prune right subtree
return root;
}
};
- From the bottom up , Pruning the subtree does not affect the structure of the nodes above , At the same time, you can traverse the whole tree without re judging a node .
class Solution {
public:
TreeNode* pre = nullptr;
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root == nullptr) return nullptr;
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
if(root->val < low){
// root->left = nullptr; // Delete left subtree
return root->right; // And replace the node with a right subtree
}else if(root->val > high){
// root->right = nullptr; // Delete the right subtree
return root->left; // And replace the node with the left subtree
}
return root;
}
};
iteration
Due to the orderliness of the search tree , Therefore, there is no need to use the stack to simulate the recursive process , The process is :
- First put the root node root Move to the range , Determine the new root node ;
- Handle unqualified nodes in the left subtree of the current root node ;
- Handle unqualified nodes in the right subtree of the current root node .
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root == nullptr) return nullptr;
// Determine the root node in the interval
while(root != nullptr){
if(root->val < low){
root = root->right;
}else if(root->val > high){
root = root->left;
}else{
break;
}
}
// here root Already in [low, high] Within the scope of , Handle left child element less than low The situation of
TreeNode* curr = root;
while(curr != nullptr){
// 【 Notice that this is while loop , Until we find the qualified > low Replace the left child node of 】
while(curr->left != nullptr && curr->left->val < low){
curr->left = curr->left->right;
}
curr = curr->left;
}
// here root Already in [low, high] Within the scope of , Deal with the right child high The situation of
curr = root;
while(curr != nullptr){
// 【 Notice that this is while loop 】
while(curr->right != nullptr && curr->right->val > high){
curr->right = curr->right->left;
}
curr = curr->right;
}
return root;
}
};
边栏推荐
- [groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)
- Debug insights
- The remainder operation is a hash function
- 中国AS树脂市场调研与投资预测报告(2022版)
- Emlog博客主题模板源码简约好看响应式
- CSDN body auto generate directory
- A survey of automatic speech recognition (ASR) research
- 2022 thinking of mathematical modeling D problem of American college students / analysis of 2022 American competition D problem
- 2022-2028 global and Chinese equipment as a Service Market Research Report
- 2022 thinking of mathematical modeling C problem of American college students / analysis of 2022 American competition C problem
猜你喜欢
![[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)](/img/1b/1fa2ebc9a6c5d271c9b39f5e508fb0.jpg)
[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)

Thinking of 2022 American College Students' mathematical modeling competition

Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?

Download the details and sequence of the original data access from the ENA database in EBI

Use assimp library to read MTL file data

AutoCAD - full screen display

Special information | real estate and office buildings - 22.1.9

质量体系建设之路的分分合合

Cookie learning diary 1

QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
随机推荐
XSS injection
775 Div.1 B. integral array mathematics
Interface joint commissioning test script optimization V5.0 (end)
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
2022-2028 global and Chinese virtual data storage Market Research Report
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Wan broadband access technology V EPON Technology
Autocad-- dynamic zoom
Function overloading
2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas
【acwing】528. cheese
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
AutoCAD - Zoom previous
An article takes you to thoroughly understand descriptors
Difference between singleton and factory pattern
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
How should programmers learn mathematics
A survey of automatic speech recognition (ASR) research
Solutions and answers for the 2021 Shenzhen cup
AutoCAD - set layer
