给定一个二叉搜索树,同时给定最小边界 low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中 (high >= low) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。
视频讲解https://www.bilibili.com/video/BV17P41177ud/?spm_id_from=333.788&vd_source=f98f2942b3c4cafea8907a325fc56a48文章讲解https://programmercarl.com/0669.%E4%BF%AE%E5%89%AA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html
- 思路:
- 递归:
- root->val < low (1 < 2)👉只能说明 root 及其左子树要被剪掉👉递归 root 右子树进行修剪
-
root->val > high (8 > 7)👉只能说明 root 及其右子树要被剪掉👉递归 root 左子树进行修剪
- 迭代:在剪枝的时候,可以分为三步:
- 将root移动到[low, high]范围内,注意是左闭右闭区间
- 剪枝左子树
- 剪枝右子树
- 代码:
// 递归
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if (root == nullptr) return nullptr;
if (root->val < low) return trimBST(root->right, low, high);
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* trimBST(TreeNode* root, int low, int high) {
if (!root) return nullptr;
// 处理头结点,让root移动到[low, high]范围内,注意是左闭右闭
while (root != nullptr && (root->val < low || root->val > high)) {
if (root->val < low) root = root->right; // 小于low往右走
else root = root->left; // 大于righ往左走
}
TreeNode *cur = root;
// 此时root已经在[low, high]范围内,处理左孩子元素小于low的情况
while (cur != nullptr) {
while (cur->left && cur->left->val < low) {
cur->left = cur->left->right;
}
cur = cur->left;
}
cur = root;
// 此时root已经在[low, high]范围内,处理右孩子大于high的情况
while (cur != nullptr) {
while (cur->right && cur->right->val > high) {
cur->right = cur->right->left;
}
cur = cur->right;
}
return root;
}
};
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
- 思路:
- 递归函数,传入nums,下标low、high,将nums [low, high)👉二叉排序树👉返回根节点
- 每次取 [low, high) 中间位置 mid 👉构造根节点
- [low, mid) 👉递归构造左子树,[mid + 1, high) 👉递归构造右子树
- 代码:
// 递归
class Solution {
public:
// 由[low, high)构造二叉搜索子树,并返回根节点
TreeNode* traversal(vector<int>& nums, int low, int high) {
if (low >= high) {
return NULL;
}
int mid = low + ((high - low) >> 1);
TreeNode* node = new TreeNode(nums[mid]);
node->left = traversal(nums, low, mid);
node->right = traversal(nums, mid + 1, high);
return node;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
return traversal(nums, 0, nums.size());
}
};
给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
视频讲解https://www.bilibili.com/video/BV1d44y1f7wP文章讲解https://programmercarl.com/0538.%E6%8A%8A%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E8%BD%AC%E6%8D%A2%E4%B8%BA%E7%B4%AF%E5%8A%A0%E6%A0%91.html
- 思路:双指针 + 反中序(右中左)
- 代码:
// 递归
class Solution {
private:
int pre = 0; // 记录前一个节点的数值
void traversal(TreeNode* cur) { // 右中左遍历
if (cur == NULL) return;
traversal(cur->right);
cur->val += pre;
pre = cur->val;
traversal(cur->left);
}
public:
TreeNode* convertBST(TreeNode* root) {
pre = 0;
traversal(root);
return root;
}
};
// 迭代
class Solution {
private:
int pre; // 记录前一个节点的数值
void traversal(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
st.push(cur);
cur = cur->right; // 右
} else {
cur = st.top(); // 中
st.pop();
cur->val += pre;
pre = cur->val;
cur = cur->left; // 左
}
}
}
public:
TreeNode* convertBST(TreeNode* root) {
pre = 0;
traversal(root);
return root;
}
};