【算法刷题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;
    }
}

总结

暂无

相关推荐
程序员南飞25 分钟前
算法-数据结构-图-邻接表构建
java·数据结构·算法·职场和发展
yngsqq26 分钟前
推导二维平面上点绕原点旋转的公式
数学·算法·机器学习·平面
Moring.33 分钟前
贪心算法
算法·贪心算法
xiaohiiii1 小时前
2022年上半年软件设计师下午题题目详解与知识点解析(附真题及答案)
java·数据库·职场和发展·uml
Ritsu栗子1 小时前
代码随想录算法训练day63---图论系列7《prim算法&kruskal算法》
c++·算法·图论
一只码代码的章鱼1 小时前
数据结构与算法-图论-最短路-单源最短路的建图方式
算法·图论
黄金龙PLUS1 小时前
十分简单的流密码算法RC4
算法·网络安全·密码学·哈希算法·同态加密
StickToForever1 小时前
第5章 软件工程(二)
经验分享·笔记·学习·职场和发展
我想吃余1 小时前
【初探数据结构】时间复杂度和空间复杂度
数据结构·算法
a_j581 小时前
算法与数据结构(环形链表II)
数据结构·算法·链表