当前位置:网站首页>669. 修剪二叉搜索树 ●●
669. 修剪二叉搜索树 ●●
2022-07-05 04:47:00 【chenyfan_】
669. 修剪二叉搜索树 ●●
给你二叉搜索树的根节点 root ,同时给定最小边界 low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在
[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。
输入:root = [3,0,4,null,2,null,null,1], low = 1, high = 3
输出:[3,2,null,1]
对于二叉搜索树,
如果 root->val < low,那么修剪后的二叉树一定出现在根节点的右边;
如果 root->val > high,那么修剪后的二叉树一定出现在根节点的左边。
递归
时间复杂度:O(N),其中 N 是给定的树的全部节点。我们最多访问每个节点一次。
空间复杂度:O(N),即使我们没有明确使用任何额外的内存,在最糟糕的情况下,我们递归调用的栈可能与节点数一样大。
- 从上往下遍历,在对某一节点进行修剪操作后,还需要重新再判断一次该节点(返回修剪后的子树);
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); // 返回修剪后的右子树
}else 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;
}
};
- 从下往上遍历,修剪子树不影响上面节点的结构,同时不需要重新判断某一节点也能遍历完成整棵树。
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; // 删除左子树
return root->right; // 并用右子树替换该节点
}else if(root->val > high){
// root->right = nullptr; // 删除右子树
return root->left; // 并用左子树替换该节点
}
return root;
}
};
迭代
由于搜索树的有序性,因此不需要用栈来模拟递归过程,处理过程为:
- 先将根节点 root 移动到区间范围内,确定新的根节点;
- 处理当前根节点左子树中不符合条件的节点;
- 处理当前根节点右子树中不符合条件的节点。
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root == nullptr) return nullptr;
// 确定在区间内的根节点
while(root != nullptr){
if(root->val < low){
root = root->right;
}else if(root->val > high){
root = root->left;
}else{
break;
}
}
// 此时root已经在[low, high] 范围内,处理左孩子元素小于low的情况
TreeNode* curr = root;
while(curr != nullptr){
// 【注意此处为while循环,直到找到符合条件 > low 的左子节点进行替换】
while(curr->left != nullptr && curr->left->val < low){
curr->left = curr->left->right;
}
curr = curr->left;
}
// 此时root已经在[low, high] 范围内,处理右孩子大于high的情况
curr = root;
while(curr != nullptr){
// 【注意此处为while循环】
while(curr->right != nullptr && curr->right->val > high){
curr->right = curr->right->left;
}
curr = curr->right;
}
return root;
}
};
边栏推荐
- [groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)
- Detailed introduction of OSPF header message
- MD5绕过
- Introduction to RT thread kernel (4) -- clock management
- Uncover the seven quirky brain circuits necessary for technology leaders
- Cookie learning diary 1
- [PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
- windows下Redis-cluster集群搭建
- Understand encodefloatrgba and decodefloatrgba
- Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
猜你喜欢

CSDN正文自动生成目录
![[Business Research Report] top ten trends of science and technology and it in 2022 - with download link](/img/9f/4fc63fa7b0e9afc5dd638d4b599b2c.jpg)
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link

Wan broadband access technology V EPON Technology
![[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)](/img/2b/933586b6feff1d48c5bee11cd734ba.jpg)
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)

2022-2028 global and Chinese virtual data storage Market Research Report

How can CIOs use business analysis to build business value?
![[Business Research Report] Research Report on male consumption trends in other economic times -- with download link](/img/08/7ea490c46c3f64af3e78d07b19b3e3.jpg)
[Business Research Report] Research Report on male consumption trends in other economic times -- with download link

How should programmers learn mathematics

SQL set operation

CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
随机推荐
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
AutoCAD - stretching
Séparation et combinaison de la construction du système qualité
可观测|时序数据降采样在Prometheus实践复盘
Solutions and answers for the 2021 Shenzhen cup
Understand encodefloatrgba and decodefloatrgba
2021 electrician cup (the 12th "China Society of electrical engineering Cup" National Undergraduate electrician mathematical modeling) detailed ideas + codes + references
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
2022 American College Students' mathematical modeling ABCDEF problem thinking /2022 American match ABCDEF problem analysis
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
Special information | real estate and office buildings - 22.1.9
Function template
English topic assignment (27)
You Li takes you to talk about C language 7 (define constants and macros)
xss注入
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
TPG x AIDU | AI leading talent recruitment plan in progress!
Flink集群配置
