刷题第22天 | 235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点

235. Lowest Common Ancestor of a Binary Search Tree

题目链接:235. Lowest Common Ancestor of a Binary Search Tree
思路链接:代码随想录二叉树-二叉搜索树的最近公共祖先

思路

这题用迭代法其实更加简单一些。用递归法就要注意将返回的节点向上返回到root的操作。遍历思路很简单,如果p,q都小于root的值,就向左遍历,如果都大于root的值,就向右遍历,如果root值处于两者中间,就直接返回当前节点,即可得到最近公共祖先。
在这里插入图片描述

Code

/**
 * 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.val >= p.val && root.val <= q.val) || root.val <= p.val && root.val >= q.val) {
            return root;
        }
        // 两者都比root小
        if (root.val > p.val && root.val > p.val) {
            return lowestCommonAncestor(root.left, p, q);
        }
        // 两者都比root大
        if (root.val < p.val && root.val < p.val) {
            return lowestCommonAncestor(root.right, p, q);
        }
        return null;
    }
}
/**
 * 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) {
            return null;
        }
        // 左
        if (root.val > p.val && root.val > q.val) {
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            if (left != null) {
                return left;
            }
        } else if (root.val < p.val && root.val < q.val) { // 右
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if (right != null) {
                return right;
            }
        }
        return root;
    }
}
/**
 * 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) {
        while (root != null) {
            if (root.val < p.val && root.val < q.val) {
                root = root.right;
            } else if (root.val > p.val && root.val > q.val) {
                root = root.left;
            } else {
                break;
            }
        }
        return root;
    }
}

701. Insert into a Binary Search Tree

题目链接:701. Insert into a Binary Search Tree
思路链接:代码随想录二叉树-二叉搜索树中的插入操作

思路

这题也简单。迭代法或递归法都比较容易,因为不需要改变二叉搜索树的结构就能完成任务。因此每一次加节点都加在叶子节点上就行。从root节点一直向下遍历,利用二叉搜索树的性质,直到找到叶子节点再添加新节点就行。注意递归法在递归操作中返回的值需要由变量来承接,这样每层都能向上返回子节点,直到root为止。
在这里插入图片描述

Code

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

450. Delete Node in a BST

题目链接:450. Delete Node in a BST
思路链接:代码随想录二叉树-删除二叉搜索树中的节点

思路

这道题如果不熟悉二叉搜索树删除节点的方法会比较难。使用递归法会比较容易。这里首先考虑删除节点时会遇到哪些情况。经过分析后,主要有五种情况:

  1. 没有搜索到要删除的节点
  2. 删除的节点是叶子节点
  3. 删除的节点的左子节点不为空,右子节点为空
  4. 删除的节点的左子节点为空,右子节点不为空
  5. 删除节点的左右子节点都不为空

因为我们还是需要先遍历到要删除的节点,因此删除节点的操作应该写在结束条件内。对于第一种情况,直接返回root,就是整棵树;对于第二种情况,直接返回null,这个null会在递归操作中被当前节点root.left/right承接;对于第三种情况,则返回左子节点;对于第四种情况,则返回右子节点。同理,第三第四种情况返回的节点都会在递归操作中被承接。

第五种情况则比较麻烦,因为需要改变二叉搜索树的结构。首先要将要找出删除节点的右节点树中最靠左的节点,然后将删除节点的左节点树移到最靠左节点的下方,成为最靠左节点的子节点树,这样才能完成删除节点的操作。最后再返回删除节点的右节点树。同理,返回的节点树在递归操作中会被承接,最终返回到递归最上层。
在这里插入图片描述

Code

/**
 * 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) {
        // 结束条件
        // 1. 没找到删的节点
        if (root == null) {
            return root;
        }
        if (root.val == key) {
            // 2. 删的节点为叶子节点
            if (root.left == null && root.right == null) {
                return null;
            } else if (root.left == null && root.right != null) { // 3.删的节点左子节点为空,右不为空
                return root.right;
            } else if (root.left != null && root.right == null) { // 4.删的节点左子节点不为空,右为空
                return root.left;
            } else { // 5.左右都不为空
                // 先找到右子节点树最靠左的节点
                TreeNode node = root.right;
                while (node.left != null) {
                    node = node.left;
                }
                // 将root(当前节点)的左子节点树移动到最靠左节点的左子节点树
                node.left = root.left;
                // 在修改后,返回root(当前节点)的右子节点树(已删除)
                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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值