题目描述
给你二叉搜索树的根节点 root
,同时给定最小边界low
和最大边界 high
。通过修剪二叉搜索树,使得所有节点的值在[low, high]
中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。
所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。
示例 1:
输入:root = [1,0,2], low = 1, high = 2
输出:[1,null,2]
示例 2:
输入:root = [3,0,4,null,2,null,null,1], low = 1, high = 3
输出:[3,2,null,1]
提示:
- 树中节点数在范围
[1, 10^4]
内 0 <= Node.val <= 10^4
- 树中每个节点的值都是 唯一 的
- 题目数据保证输入是一棵有效的二叉搜索树
0 <= low <= high <= 10^4
思路
递归法
递归三部曲:
- 确定递归函数的参数和返回值。因为是要遍历整棵树,做修改,其实不需要返回值也可以完成修剪(其实就是从二叉树中移除节点)的操作。但是有返回值,更方便,可以通过递归函数的返回值来移除节点。
- 确定终止条件。修剪的操作并不是在终止条件上进行的,遇到空节点返回就可以了。
- 确定单层递归的逻辑。如果root(当前节点)的元素小于low的数值,那么应该递归右子树,并返回右子树符合条件的头结点。如果root(当前节点)的元素大于high的,那么应该递归左子树,并返回左子树符合条件的头结点。
迭代法
因为二叉搜索树的有序性,不需要使用栈模拟递归的过程。
在剪枝的时候,可以分为三步:
- 将root移动到[low, high] 范围内,注意是左闭右闭区间
- 剪枝左子树
- 剪枝右子树
代码
C++版:
递归法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
// 递归法
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root==NULL) return NULL;
if(root->val<low){
// 此时左子树的都是不符合的节点
TreeNode* right = trimBST(root->right, low, high); // 寻找符合区间[low, high]的节点
return right;
}
if (root->val > high) {
TreeNode* left = trimBST(root->left, low, high); // 寻找符合区间[low, high]的节点
return left;
}
root->left = trimBST(root->left, low, high); // root->left接入符合条件的左孩子
root->right = trimBST(root->right, low, high); // root->right接入符合条件的右孩子
return root;
}
};
迭代法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
// 迭代法
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root==NULL) return NULL;
// 处理头节点,让root移动到[low, high]范围内,注意是左闭右闭
while(root!=NULL && (root->val<low || root->val>high)){
if (root->val < low) root = root->right; // 小于L往右走
else root = root->left; // 大于R往左走
}
TreeNode *cur = root;
// 一路向左,处理左孩子元素小于low的情况
while(cur!=NULL){
while(cur->left!=NULL && cur->left->val<low){
cur->left=cur->left->right; // 左孩子的右子树中可能会有符合范围的节点
}
cur=cur->left;
}
cur = root;
// 一路向右,处理左孩子元素大于high的情况
while(cur!=NULL){
while(cur->right!=NULL && cur->right->val>high){
cur->right=cur->right->left; // 右孩子的左子树中可能会有符合范围的节点
}
cur=cur->right;
}
return root;
}
};
Python版:
递归法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
# 递归法
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if root is None:
return None
if root.val < low:
# 寻找符合区间 [low, high] 的节点
return self.trimBST(root.right, low, high)
if root.val > high:
# 寻找符合区间 [low, high] 的节点
return self.trimBST(root.left, low, high)
root.left = self.trimBST(root.left, low, high) # root.left 接入符合条件的左孩子
root.right = self.trimBST(root.right, low, high) # root.right 接入符合条件的右孩子
return root
迭代法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
# 迭代法
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if not root:
return None
# 处理头节点,让root移动到[low, high] 范围内,注意是左闭右闭
while root and (root.val < low or root.val > high):
if root.val < low:
root = root.right # 小于L往右走
else:
root = root.left # 大于R往左走
cur = root
# 处理左孩子元素小于low的情况
while cur:
while cur.left and cur.left.val < low:
cur.left = cur.left.right
cur = cur.left
cur = root
# 处理右孩子大于high的情况
while cur:
while cur.right and cur.right.val > high:
cur.right = cur.right.left
cur = cur.right
return root