当前位置:网站首页>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;
}
};
边栏推荐
- Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
- How should programmers learn mathematics
- 揭秘技术 Leader 必备的七大清奇脑回路
- Practice | mobile end practice
- 49 pictures and 26 questions explain in detail what is WiFi?
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- 猿人学第一题
- Séparation et combinaison de la construction du système qualité
- Flutter tips: various fancy nesting of listview and pageview
- Minor spanning tree
猜你喜欢

Web开发人员应该养成的10个编程习惯
![Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar](/img/bf/fb4e85143d1461a2026c88cda4a18d.jpg)
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar

windows下Redis-cluster集群搭建

質量體系建設之路的分分合合

Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
![[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens](/img/20/5c5550e6dabc76702f0e7ce3bef068.jpg)
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens

xss注入

Invalid bound statement (not found) in idea -- problem solving

2022 U.S. college students' mathematical modeling e problem ideas / 2022 U.S. game e problem analysis

取余操作是一个哈希函数
随机推荐
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
Leetcode hot topic Hot 100 day 33: "subset"
Understand encodefloatrgba and decodefloatrgba
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)
Error statuslogger log4j2 could not find a logging implementation
2022 thinking of mathematical modeling C problem of American college students / analysis of 2022 American competition C problem
Pointer function (basic)
CSDN正文自动生成目录
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
Basic analysis of IIC SPI protocol
windows下Redis-cluster集群搭建
Reading and visualization of DICOM, MHD and raw files in medical imaging
The difference between bundle, chunk and module
Detailed introduction of OSPF header message
[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
Chapter 6 text processing tools for shell programming (awk)
775 Div.1 C. Tyler and strings combinatorial mathematics
