代码随想录算法训练营第二十二天| LeetCode235. 二叉搜索树的最近公共祖先、LeetCode701.二叉搜索树中的插入操作、LeetCode450.删除二叉搜索树中的节点

#LeetCode 235. Lowest Common Ancestor of a Binary Search Tree

#LeetCode 235. 视频讲解:二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先_哔哩哔哩_bilibili

根据二叉树的特性,如果一个节点的值处于两个目标值的之间,那么说明这个节点一定是最小公共祖先。题目规定所寻找的节点一定存在树中,所以只需要考虑向那一侧进行寻找。在递归法中,终止条件为遇到空值即可返回。

递归法代码:

/**
 * 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) {
        return traversal(root, p, q);
    }
    public TreeNode traversal(TreeNode root, TreeNode p, TreeNode q) { 
        if (root == null) {
            return null;
        }
        if (root.val < p.val && root.val < q.val) {
            TreeNode right = traversal(root.right, p, q);
            if (right != null) {
                return right;
            }
        } else if (root.val > p.val && root.val > q.val) {
            TreeNode left = traversal(root.left, p, q);
            if (left != null) {
                return left;
            }
        }
        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(true) {
            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;
    }
}

#LeetCode 701. Insert into a Binary Search Tree

#LeetCode 701. 视频讲解:原来这么简单? | LeetCode:701.二叉搜索树中的插入操作_哔哩哔哩_bilibili

题目较简单,只需要插入在叶子节点就可以。

递归三部曲:

1. 确定递归函数参数以及返回值:参数是根节点,返回值是节点TreeNode(利用返回值完成新加入的节点与其父节点的赋值操作)。

2. 确定终止条件:节点为null。

3. 确定单层递归的逻辑:因为是二叉搜索树,所以根据插入的值,我们不需要遍历整个树,只需要比较当前节点与目标值的大小,选择左子树或者右子树继续遍历。

使用节点的left 或者right 节点接住新节点,所以设置了返回值。

迭代法代码:

/**
 * 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) {
            TreeNode node = new TreeNode(val);
            return node;
        }
        if (val < root.val) {
            root.left = insertIntoBST(root.left, val);
        }
        if (val > root.val) {
            root.right = insertIntoBST(root.right, val);
        }
        return root;
    }
}

#LeetCode 450. Delete Node in a BST

#LeetCode 450. 视频讲解:调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点_哔哩哔哩_bilibili

本题的重点是分析出可能会出现的5 种情况:

1. 没有找到需要删除的节点,则返回root 节点。

2. 删除的节点是叶子节点,需要将叶子节点的父节点的对应left 或者right指向null。

3. 左叶子节点不空,右叶子节点为空,当前节点的左孩子作为当前节点父节点的左孩子。

4. 左叶子节点为空,右叶子节点不空,当前节点的右孩子作为当前节点父节点的右孩子。

5. 左叶子节点不空,右叶子节点不空,需要找到删除节点右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点,如图。

递归法代码:

/**
 * 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 null;
        }
        if (root.val == key) {
            if (root.left == null && root.right == null) { // leaf
                return null;
            }
            else if (root.left != null && root.right == null) {
                return root.left;
            }
            else if (root.left == null && root.right != null) {
                return root.right;
            }
            else {
                TreeNode cur = root.right;
                while(cur.left != null) {
                    cur = cur.left;
                }
                cur.left = root.left;
                return root.right;
            }
        }
        // recursive
        if (key < root.val) {
            root.left = deleteNode(root.left, key);
        }
        else if (key > root.val) {
            root.right = deleteNode(root.right, key);
        }
        return root;
    }
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值