• LeetCode刷题day22||235. 二叉搜索树的最近公共祖先&&701.二叉搜索树中的插入操作&&450.删除二叉搜索树中的节点--二叉树


    235. 二叉搜索树的最近公共祖先

    题目描述

    在这里插入图片描述
    题目链接

    思路分析

    本题是二叉搜索树,二叉搜索树是有序的,那得好好利用一下这个特点。
    只要从上到下去遍历,遇到 cur节点是数值在[p, q]区间中则一定可以说明该节点cur就是q 和 p的公共祖先。

    代码

    class Solution {
    private:
        TreeNode* traversal(TreeNode* root, TreeNode* p, TreeNode* q) {
            if (root == NULL) return root;
            if (root->val > q->val && root->val > p->val) {
                TreeNode* left = traversal(root->left, p, q);
                if (left != NULL) {
                    return left;
                }
            }
            if (root->val < q->val && root->val < p->val) {
                TreeNode* right = traversal(root->right, p, q);
                if (right != NULL) {
                    return right;
                }
            }
            return root;
        }
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
            return traversal(root, p, q);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    class Solution {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
            if (root->val > q->val && root->val > p->val) return lowestCommonAncestor(root->left, p, q);
            if (root->val < q->val && root->val < p->val) return lowestCommonAncestor(root->right, p, q);
            else return root;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    迭代法

    class Solution {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
            while(root) {
                if (root->val > p->val && root->val > q->val) {
                    root = root->left;
                } else if (root->val < p->val && root->val < q->val) {
                    root = root->right;
                } else return root;
            }
            return NULL;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    701.二叉搜索树中的插入操作

    题目描述

    在这里插入图片描述
    在这里插入图片描述
    题目链接

    思路分析

    只要按照二叉搜索树的规则去遍历,遇到空节点就插入节点就可以了。

    代码

    class Solution {
    public:
        TreeNode* insertIntoBST(TreeNode* root, int val) {
            if (root == NULL) {
                TreeNode* node = new TreeNode(val);
                return node;
            }
            if (root->val > val) root->left = insertIntoBST(root->left, val);
            if (root->val < val) root->right = insertIntoBST(root->right, val);
            return root;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    class Solution {
    private:
        TreeNode* parent;
        void traversal(TreeNode* root, int val) {
            if (root == NULL) {
                TreeNode* node = new TreeNode(val);
                if (val > parent->val) parent->right = node;
                else parent->left = node;
                return;
            }
            parent = root;
            if (val > root->val) traversal(root->right, val);
            if (val < root->val) traversal(root->left, val);
            return;
        }
    public:
        TreeNode* insertIntoBST(TreeNode* root, int val) {
            parent = new TreeNode(0);
            if (root == NULL) {
                root = new TreeNode(val);
            }
            traversal(root, val);
            return root;
    
    
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    450.删除二叉搜索树中的节点

    题目描述

    在这里插入图片描述

    在这里插入图片描述
    题目链接

    思路分析

    二叉搜索树的删除

    有以下五种情况:

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

    代码

    class Solution {
    public:
        TreeNode* deleteNode(TreeNode* root, int key) {
            if (root == NULL) return nullptr;
            if (root->val == key) {
                if (root->left == NULL && root->right == NULL) {
                    delete root;
                    return nullptr;
                }else if (root->left == nullptr) {
                    auto retNode = root->right;
                    delete root;
                    return retNode;
                }else if (root->right == nullptr) {
                    auto retNode = root->left;
                    delete root;
                    return retNode;
                }else {
                    TreeNode* cur = root->right;
                    while (cur->left != nullptr) cur = cur->left;
                    cur->left = root->left;
                    TreeNode* tmp = root;
                    root = root->right;
                    delete tmp;
                    return root;
                }
            }
            if (root->val < key) root->right = deleteNode(root->right, key);
            if (root->val > key) root->left = deleteNode(root->left, key);
            return root;
    
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    【数据结构】顺序表的增删查改 (C语言实现)
    机器学习中常用的评价指标
    建筑能源管理(2)——建筑用能分类与计算方法
    第149篇 笔记-web3
    CFdiv2-Two Pizzas-(预处理+状态压缩)
    ArcGIS Pro怎么进行挖填方计算
    2022年比若依更香的开源项目
    动态规划 | 不同路径、整数拆分、不同的二叉搜索树 | leecode刷题笔记
    Ajax技术
    leetcode76 Minimum Window Substring
  • 原文地址:https://blog.csdn.net/wangjianhs/article/details/127541805