leetcode 235. 二叉搜索树的最近公共祖先
题目描述
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]

示例
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
题目链接
https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree
解题思路
一种方法是将其看作普通二叉树来做,但这样就没用上二叉搜索树的性质。
另一种,根据性质来做。
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        int min = Math.min(p.val, q.val), max = Math.max(p.val, q.val);
        if (root.val < min) {
            return lowestCommonAncestor(root.right, p, q);
        } else if (root.val > max) {
            return lowestCommonAncestor(root.left, p, q);
        }
        return root;
    }
}
leetcode 701. 二叉搜索树中的插入操作
题目描述
给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。
示例

输入:root = [4,2,7,1,3], val = 5
输出:[4,2,7,1,3,5]
解释:另一个满足题目要求可以通过的树是:
题目链接
https://leetcode.cn/problems/insert-into-a-binary-search-tree
解题思路
首先找到插入位置的上一个节点,按照二叉搜索树性质来。找第一个大于它的。然后插入对应位置。
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return new TreeNode(val);
        TreeNode node = getNode(null, root, val);
        if (val < node.val) node.left = new TreeNode(val);
        if (val > node.val) node.right = new TreeNode(val);
        return root;
    }
    public TreeNode getNode(TreeNode prev, TreeNode cur, int val) {
        if (cur == null) return prev;
        prev = cur;
        if (val < cur.val) {
            return getNode(prev, cur.left, val);
        } else {
            return getNode(prev, cur.right, val);
        }
    }
}
leetcode 450. 删除二叉搜索树中的节点
题目描述
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
- 首先找到需要删除的节点;
 - 如果找到了,删除它。
 
示例

输入:root = [5,3,6,2,4,null,7], key = 3
输出:[5,4,6,2,null,null,7]
解释:给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。
一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。
另一个正确答案是 [5,2,6,null,4,null,7]。
题目链接
https://leetcode.cn/problems/delete-node-in-a-bst/
解题思路
分情况讨论
- 如果 
key < root.val,去左子树删除节点 - 如果 
key > root.val,去右子树删除节点 - 如果相等,则就是要删除的节点。再分情况讨论
- 左子树为空。
cur = cur.right - 右子树为空。
cur = cur.left - 有左右子树。左子树转移到右子树的最左叶子结点的左节点;当前节点置为右子树根节点。
 
 - 左子树为空。
 
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.right;
            while (cur.left != null) {
                cur = cur.left;
            }
            cur.left = root.left;
            root = root.right;
            return root;
        }
    }
    if (root.val > key) root.left = deleteNode(root.left, key);
    if (root.val < key) root.right = deleteNode(root.right, key);
    return root;
  }
}
                
            

评论