day 21 二叉搜索树

二叉搜索树
1.二叉树性质查找
700. 二叉搜索树中的搜索
2 二叉搜索树的插入和删除
701. 二叉搜索树中的插入操作
3. 利用二叉树性质找差值
98. 验证二叉搜索树
530. 二叉搜索树的最小绝对差
501. 二叉搜索树中的众数
框架

    TreeNode *pre = nullptr;  
    void BST(TreeNode* root) {
        if(!root) return true;
        BST(root->left);
        if(pre)处理相邻两个值的关系
        pre = root;
        BST(root->right); 
    }

700. 二叉搜索树中的搜索

class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        if(!root) return nullptr;
        if(val > root->val){
            return searchBST(root->right, val);
        }else if(val < root->val){
            return searchBST(root->left, val);
        }else if(val == root->val) return root;
        return nullptr;
    }
};

98. 验证二叉搜索树
首先是必须是中序遍历

  1. 中序遍历,将数值存到数组中,判断数组是否有序
  2. 中序遍历直接判断,当前值是否比上一个出栈的值大
class Solution {
public:
    TreeNode *pre = nullptr;
    bool isValidBST(TreeNode* root) {
        if(!root) return true;
        bool left = isValidBST(root->left);
        if(pre && root->val <= pre->val) return false;
        pre = root;
        bool right = isValidBST(root->right);
        return left && right;

    }

};

701. 二叉搜索树中的插入操作
递归函数有返回值的好处是 可以利用返回值完成新加入的节点与其父节点的赋值操作

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(!root) return new TreeNode(val);
        if(root->val > val) {
            root->left = insertIntoBST(root->left, val);
        }
         if(root->val < val){
             root->right =  insertIntoBST(root->right, val);
         }
        return root;
    }
};

450. 删除二叉搜索树中的节点
第一种情况:没找到删除的节点,遍历到空节点直接返回了
找到删除的节点
第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(!root) return root;
        if(root->val == key){
            if(!root->left && !root->right) return nullptr;
            if(!root->right) return root->left;
            if(!root->left) return root->right;
            TreeNode* temp = root->right;
            while(temp->left){
                temp = temp->left;
            }
            temp->left = root->left;
            return root->right;
        }
        if(root->val > key){
            root->left = deleteNode(root->left, key);
        }
        if(root->val < key){
            root->right = deleteNode(root->right, key);
        }
        return root;
    }
};

530. 二叉搜索树的最小绝对差

class Solution {
public:
    TreeNode* pre = nullptr;
    int min = INT_MAX ;
    int getMinimumDifference(TreeNode* root) {
        dfs(root);
        return min;
    }
    void dfs(TreeNode *root){
        if(!root) return;
        dfs(root->left);
        if(pre){
            if(min > root->val - pre->val){
                min = root->val - pre->val;
            }
        }
        pre = root;
        dfs(root->right);
    }
};

501. 二叉搜索树中的众数
如果 频率count 等于 maxCount(最大频率),当然要把这个元素加入到结果集中
频率count 大于 maxCount的时候,不仅要更新maxCount,而且要清空结果集

class Solution {
public:
    TreeNode* pre = nullptr;
    int count = 1;
    int max = -1;
    vector<int>result;
    vector<int> findMode(TreeNode* root) {
        dfs(root);
        return result;
    }

    void dfs(TreeNode* root){
        if(!root) return;
        dfs(root->left);
        if(pre){
            if(root->val == pre->val){
                count++;
            }else{
                count = 1;
            }
        }
        pre = root;
        cout<<root->val <<" "<<count<< endl;
        if(count == max){
            result.push_back(root->val);
        }else if(count > max){
            result.clear();
            max = count;
            result.push_back(root->val);
        }
        dfs(root->right);
    }
};

669. 修剪二叉搜索树

class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
            if(!root) return root;
            if(root->val < low){
                return trimBST(root->right, low, high);
            }
            else if(root->val > high){
                return trimBST(root->left, low, high);
            }
            else{
                root->left = trimBST(root->left, low, high);
                root->right = trimBST(root->right, low, high);
            }
            return root;

    }
};

108. 将有序数组转换为二叉搜索树

class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        TreeNode* root = build(nums, 0, nums.size()-1);
        return root;
    }
    TreeNode* build(vector<int>& nums, int left, int right){
            if(left > right) return nullptr;
            int mid = left + (right - left)/2;
            TreeNode * root = new TreeNode(nums[mid]);
            root->left = build(nums, left, mid-1);
            root->right = build(nums, mid+1, right);
            return root;
    }
};

538. 把二叉搜索树转换为累加树
将它的每个节点的值替换成树中大于或者等于该节点值的所有节点值之和。
逆中序遍历来求累加和

class Solution {
public:
    int sum = 0;
    TreeNode* convertBST(TreeNode* root) {
        if(!root) return nullptr;
        root->right = convertBST(root->right);
        sum += root->val;
        root->val = sum;
        root->left = convertBST(root->left);
        return root;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值