1.修建二叉搜索树
题目链接/文章讲解: 代码随想录
视频讲解: 你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树_哔哩哔哩_bilibili
代码:递归法
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root == NULL) return NULL;
// 中
if(root->val > high){
return trimBST(root->left,low,high);
}
if(root->val < low){
return trimBST(root->right,low,high);
}
// 左
root->left = trimBST(root->left,low,high);
// 右
root->right = trimBST(root->right,low,high);
return root;
}
};
note:这里的易错点是,当我们遇到要删除的结点时,会根据情况舍弃它的左子树或右子树,但是还是要递归调用函数来去修剪我们留下来的那颗子树。最后不要忘了更新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) return nullptr;
while(root != nullptr && (root->val < low || root->val > high)){
if(root->val < low) root = root->right;
else root = root->left;
}
TreeNode* cur = root;
while(cur != nullptr){
while(cur->left && cur->left->val < low){
cur->left = cur->left->right;
}
cur = cur->left;
}
cur = root;
while(cur != nullptr){
while(cur->right && cur->right->val > high){
cur->right = cur->right->left;
}
cur = cur->right;
}
return root;
}
};
note:迭代法其实就是不断地剪枝。二叉搜索树的迭代法都不需要用到栈。
2.将有序数组转化为二叉搜索树
代码:递归法
/**
* 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 {
private: // 左闭右闭
TreeNode* traversal(vector<int> &nums,int left,int right){
// 递归出口
if(left > right) return nullptr;
int mid = left + (right - left) / 2;
// 中
TreeNode* node = new TreeNode(nums[mid]);
// 左
node->left = traversal(nums,left,mid - 1);
// 右
node->right = traversal(nums,mid + 1,right);
return node;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return traversal(nums,0,nums.size() - 1);
}
};
note:注意这里左右递归的时候的参数 是left mid -1 和 mid + 1 right
3.将二叉搜索树转变为累加树
代码:递归法
/**
* 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 {
private:
int pre = 0;
void traversal(TreeNode* node){
if(node == NULL) return;
// 右
traversal(node->right);
// 中
node->val += pre;
pre = node->val;
// 左
traversal(node->left);
}
public:
TreeNode* convertBST(TreeNode* root) {
traversal(root);
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* convertBST(TreeNode* root) {
stack<TreeNode*>st;
if(root == NULL) return NULL;
int pre = 0;
st.push(root);
while(!st.empty()){
TreeNode* node = st.top();
if(node != NULL){
st.pop();
// 左
if(node->left) st.push(node->left);
// 中
st.push(node);
st.push(nullptr);
// 右
if(node->right) st.push(node->right);
}else{
st.pop();
node = st.top();
st.pop();
node->val += pre;
pre = node->val;
}
}
return root;
}
};
note:我之前想着拿函数参数传值,,,然后就顺理成章地失败了。不如双指针。
以及这个中序遍历的倒叙版也是。。get到了