【算法刷题day22】Leetcode:235. 二叉搜索树的最近公共祖先、701. 二叉搜索树中的插入操作、450. 删除二叉搜索树中的节点

文章目录

    • [Leetcode 235. 二叉搜索树的最近公共祖先](#Leetcode 235. 二叉搜索树的最近公共祖先)
    • [Leetcode 701. 二叉搜索树中的插入操作](#Leetcode 701. 二叉搜索树中的插入操作)
    • [Leetcode 450. 删除二叉搜索树中的节点](#Leetcode 450. 删除二叉搜索树中的节点)

草稿图网站
java的Deque

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

题目: 235. 二叉搜索树的最近公共祖先
解析: 代码随想录解析

解题思路

法1:和人二叉树的最近公共祖先一样的遍历搜索方法。

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root == p || root == q)
            return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left == null &&right == null)
            return null;
        else if (left != null && right == null)
            return left;
        else if (left == null && right != null)
            return right;
        else
            return root;
    }
}

//递归
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (p.val < root.val && q.val < root.val)   return lowestCommonAncestor(root.left, p, q);
        if (p.val > root.val && q.val > root.val)   return lowestCommonAncestor(root.right, p, q);
        return root;
    }
}

//迭代法
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode cur = root;
        while (true) {
            if (p.val < cur.val && q.val < cur.val)
                cur = cur.left;
            else if (p.val > cur.val && q.val > cur.val)
                cur = cur.right;
            else
                break;
        }
        return cur;
    }
}

总结

利用好二叉搜索树的有序性

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

题目: 701. 二叉搜索树中的插入操作
解析: 代码随想录解析

解题思路

迭代和递归

代码

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 insertIntoBST(TreeNode root, int val) {
        if (root == null)
            return new TreeNode(val);
        TreeNode pre = null;
        TreeNode cur = root;
        while (cur != null) {
            if (val < cur.val) {
                pre = cur;
                cur = cur.left;
            } else if (val > cur.val) {
                pre = cur;
                cur = cur.right;
            }
            
        }
        if (val < pre.val)
            pre.left = new TreeNode(val);
        else
            pre.right = new TreeNode(val);
        return root;
    }
}

//递归
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null)
            return new TreeNode(val);
        if (val < root.val)
            root.left = insertIntoBST(root.left, val);
        if (val > root.val)
            root.right = insertIntoBST(root.right, val);
        return root;
    }
}

总结

根据二叉搜索树的性质

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

题目: 450. 删除二叉搜索树中的节点
解析: 代码随想录解析

解题思路

应该为左为空,右为空,左右都不为空的情况

代码

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 deleteNode(TreeNode root, int key) {
        if (root == null)
            return root;
        if (root.val == key) {
            if (root.left == null)
                return root.right;
            else if (root.right == null)
                return root.left;
            else{
                TreeNode cur = root.left;
                while (cur.right != null)
                    cur = cur.right;
                cur.right = root.right;
                root = root.left;
                return root;
            }
        }
        if (key < root.val) root.left = deleteNode(root.left, key);
        if (key > root.val) root.right = deleteNode(root.right, key);
        return root;
    }
}

总结

暂无

相关推荐
Once_day1 分钟前
代码训练LeetCode(24)数组乘积
算法·leetcode
独行soc1 小时前
2025年渗透测试面试题总结-腾讯[实习]玄武实验室-安全工程师(题目+回答)
linux·安全·web安全·面试·职场和发展·渗透测试·区块链
int型码农2 小时前
数据结构第八章(二)-交换排序
c语言·数据结构·算法·排序算法
YKPG2 小时前
C++学习-入门到精通【14】标准库算法
c++·学习·算法
CoovallyAIHub2 小时前
AI+无人机如何守护濒危物种?YOLOv8实现95%精准识别
深度学习·算法·计算机视觉
码农之王3 小时前
记录一次,利用AI DeepSeek,解决工作中算法和无限级树模型问题
后端·算法
·云扬·3 小时前
【PmHub面试篇】Gateway全局过滤器统计接口调用耗时面试要点解析
面试·职场和发展·gateway
编程绿豆侠5 小时前
力扣HOT100之二分查找: 34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode
Shan12055 小时前
找到每一个单词+模拟的思路和算法
数据结构·算法
国家不保护废物5 小时前
微信红包算法深度解析:从产品思维到代码实现
javascript·算法·面试