二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。
二叉查找树中序遍历有序。
给你二叉搜索树的根节点 root
,同时给定最小边界low
和最大边界 high
。通过修剪二叉搜索树,使得所有节点的值在[low, high]
中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。
所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。
输入:root = [1,0,2], low = 1, high = 2
输出:[1,null,2]
输入:root = [3,0,4,null,2,null,null,1], low = 1, high = 3
输出:[3,2,null,1]
提示:
法一:递归:
任意一个节点的val
,对给定的范围只有三种可能,等于、小于、大于
法二:迭代:
该题自然能够使用「迭代」进行求解:起始先从给定的 root
进行出发,找到第一个满足值符合 [low,high]
范围的节点,该节点为最后要返回的真正的根节点 root
。
然后分别处理root
节点的左子树和右子树:
法一:递归:
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
if(root == null) return null;
if(root.val >= low && root.val <= high){
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
}else if(root.val < low){
return trimBST(root.right, low, high);
}else if(root.val > high){
return trimBST(root.left, low, high);
}
return root;
}
}
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 == nullptr) return nullptr;
if(root->val >= low && root->val <= high){
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
}else if(root->val < low){
return trimBST(root->right, low, high);
}else if(root->val > high){
return trimBST(root->left, low, high);
}
return root;
}
};
法二:迭代:
Java
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
while(root != null && (root.val < low || root.val > high)){
root = root.val < low ? root.right : root.left;
}
if(root == null) return null;
TreeNode tem = root;
while(tem.left != null){
if(tem.left.val >= low){
tem = tem.left;
}else{
tem.left = tem.left.right;
}
}
tem = root;
while(tem.right != null){
if(tem.right.val <= high){
tem = tem.right;
}else{
tem.right = tem.right.left;
}
}
return root;
}
}
C++
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
while(root != nullptr && (root->val < low || root->val > high)){
root = root->val < low ? root->right : root->left;
}
if(root == nullptr) return nullptr;
TreeNode* tem = root;
while(tem->left != nullptr){
if(tem->left->val >= low){
tem = tem->left;
}else{
tem->left = tem->left->right;
}
}
tem = root;
while(tem->right != nullptr){
if(tem->right->val <= high){
tem = tem->right;
}else{
tem->right = tem->right->left;
}
}
return root;
}
};
n
为二叉树的结点数目。题目来源:力扣。
放弃一件事很容易,每天能坚持一件事一定很酷,一起每日一题吧!
关注我 leetCode专栏,每日更新!