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

目录

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

1、递归实现

2、迭代法实现

 701.二叉搜索树中的插入操作(递归实现)

450.删除二叉搜索树中的节点(递归实现)


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

相对于 二叉树的最近公共祖先 本题就简单一些了,因为 可以利用二叉搜索树的特性。

题目链接/文章讲解:代码随想录

题解思路:

1、递归实现

迭代法和递归法实现的原理一致,其实我觉得这种迭代法本质就是递归的方法,只不过步骤不一样而已!!!

/**
 * 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){
                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;
            }else{
                return root;
            }
        }
        return null;
    }
}

2、迭代法实现

迭代法和递归法实现的原理一致,其实我觉得这种迭代法本质就是递归的方法,只不过步骤不一样而已!!!

扫描二维码关注公众号,回复: 15162761 查看本文章
/**
 * 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){
                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;
            }else{
                return root;
            }
        }
        return null;
    }
}

 701.二叉搜索树中的插入操作(递归实现)

本题比想象中的简单,大家可以先自己想一想应该怎么做,然后看视频讲解,就发现 本题为什么比较简单了。

题目链接/文章讲解:代码随想录

题解思路:

多听卡哥视频讲解!!!在二叉树中插入操作就是构造二叉树,一定能在叶子节点找到我们要插入的节点,然后可以根据二叉搜索树的特性比较当前节点的val值和给定的val,直接判断在其左子树还是右子树进行插入操作!!!最后直接返回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);;

        //第三步:确定单层处理逻辑
        if(root.val > val){
            root.left = insertIntoBST(root.left,val); //直接根据二叉搜索树的特性比较当前节点的val值和给定的val,直接判断在其左子树还是右子树进行插入操作
        }else if(root.val < val){
            root.right = insertIntoBST(root.right,val); //直接根据二叉搜索树的特性比较当前节点的val值和给定的val,直接判断在其左子树还是右子树进行插入操作
        }

        return root;
    }

    
}

作者:vansven-h
链接:https://leetcode.cn/problems/insert-into-a-binary-search-tree/solution/701er-cha-sou-suo-shu-zhong-de-cha-ru-ca-fj39/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

450.删除二叉搜索树中的节点(递归实现)

相对于 插入操作,本题就有难度了,涉及到改树的结构

题目链接/文章讲解:代码随想录

题解思路:

主要是删除节点的情况存在五种不同情况,需要根据不同情况进行一一处理,具体的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; //第一种情况:没找到要删除的节点,直接返回null值即可
        else if(root.val == key){     //以下四种情况都是在找到要删除节点的情况下进行讨论的
            if(root.left == null && root.right == null) 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 current = root.right;  //第五种情况:删除左不为空,右不为空的节点,根据二叉搜索树的特性,需要一直搜索右子树的左节点,一直其叶节点进行插入
                while( current.left != null){
                    current = current.left;
                }
                current.left = root.left;
                return root.right;
            }
        }

        //第三步:确定单层处理逻辑,根据二叉树的搜索特性,直接比较当前遍历节点的val值和key值,直接判断在其左子树还是右子树进行删除节点操作
        if(root.val > key) root.left = deleteNode(root.left,key);
        else if(root.val < key) root.right = deleteNode(root.right,key);

        return root;
    }
}

猜你喜欢

转载自blog.csdn.net/tore007/article/details/130628721