当前位置:网站首页>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;
}
};
边栏推荐
- Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
- 官宣!第三届云原生编程挑战赛正式启动!
- Emlog blog theme template source code simple good-looking responsive
- CSDN body auto generate directory
- [groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)
- Pointer function (basic)
- Function overloading
- The principle of attention mechanism and its application in seq2seq (bahadanau attention)
- Flink cluster configuration
- 程序员应该怎么学数学
猜你喜欢
Download the details and sequence of the original data access from the ENA database in EBI
揭秘技术 Leader 必备的七大清奇脑回路
Flutter tips: various fancy nesting of listview and pageview
Construction d'un Cluster redis sous Windows
windows下Redis-cluster集群搭建
Cookie learning diary 1
The remainder operation is a hash function
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
Setting up redis cluster cluster under Windows
Key review route of probability theory and mathematical statistics examination
随机推荐
Neural networks and deep learning Chapter 2: machine learning overview reading questions
【acwing】240. food chain
[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)
自动语音识别(ASR)研究综述
Fluent objects and lists
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
Interface joint commissioning test script optimization V5.0 (end)
Variable category (automatic, static, register, external)
2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens
托管式服务网络:云原生时代的应用体系架构进化
3 minutes learn to create Google account and email detailed tutorial!
#775 Div.1 B. Integral Array 数学
On-off and on-off of quality system construction
AutoCAD -- dimension break
猿人学第一题
Special information | finance, accounting, audit - 22.1.23
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Pointer function (basic)
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions