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

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

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

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

/**
 * 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) {
        //若传入二叉树为空,则直接返回null
        if(root == null){
            return new TreeNode(val);
        }
        //双指针法初始化
        //root要参与双指针遍历,用newRoot记录二叉树根结点
        TreeNode newRoot = root;
        //pre与root做双指针遍历
        TreeNode pre = root;
        //若未找到插入位置,则一直进行双指针循环
        while(root != null){
            pre = root;
            if(root.val > val){
                root = root.left;
            }else if(root.val < val){
                root = root.right;
            }
        }
        //root指针遍历到了插入位置
        //此时root==null,pre为前一个结点的位置
        if(pre.val > val){
            //应插入到pre的左分支
            pre.left = new TreeNode(val);
        }else{
            //,比pre.val大,插入到右分支(题中元素不会有重复)
            pre.right = new TreeNode(val);
        }
        //返回添加结点后的二叉树
        return newRoot;
    }
}

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

(当待删除结点左右分支均不为空时的删除操作)

  • 题解(递归法):
/**
 * 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) {
        root = delete(root, key);
        return root;
    }
    public TreeNode delete(TreeNode root, int key){
        //若传入树为空,则直接返回null
        if(root == null){
            return null;
        }
        if(root.val > key){
            // 若待删值在root左分支,进行左递归
            //并承接下一层递归的返回值
            root.left = delete(root.left, key);
        }else if(root.val < key){
            //若待删值在root右分支,进行右递归
            //并承接下一层递归的返回值
            root.right = delete(root.right, key);
        }else{
            //若找到待删结点
            //情况1、若待删结点左子树为空右子树不为空,
            //则直接返回当前结点右分支给root的上一层,删掉root,完成处理
            if(root.left == null && root.right != null){
                return root.right;
            }
            //情况2、若待删结点子右树为空左子树不为空,
            //则直接返回当前结点左分支给root的上一层,删掉root,完成处理
            if(root.left != null && root.right == null){
                return root.left;
            }
            //情况3、若待删结点左右子树均为空,则直接返回null给root上一层,完成删除
            if(root.left == null && root.right == null){
                return null;
            }
            //情况4、若待删结点左右子树均不为空,要保证删除后仍然满足搜索树,需要进一步处理
            //找到当前root右分支中的最左结点,这个结点就是最接近root的比root大的结点
            TreeNode temp = root.right;
            while(temp.left != null){
                temp = temp.left;
            }
            //当前,temp指向了root右分支的最左结点,而temp.left == null
            //把原root的左分支加到找到的temp结点的右分支上
            temp.left = root.left;
            //将原root的右分支的顶到root的位置上,完成删除操作
            root = root.right;
        }
        return root;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值