最全数据结构与算法 二叉搜索树_层次化结构从小到大排序node grid(3),惊喜

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

在这里插入图片描述

public TreeNode searchBST(TreeNode root, int val) {
        if(root==null) return null;
        TreeNode curr=root;
        while(curr!=null){
            if(curr.val==val){
                return curr;
            }else if(curr.val<val){
                curr=curr.right;
            }else{
                curr=curr.left;
            }
        }
        //循环外面说明没有找到
        return null;
    }

🍊3.二叉搜索树的插入

NC372 插入二叉搜索树

二叉搜索树的插入需要考虑下面几种情况

1️⃣根节点为空,那么待插入的节点就作为二叉搜索树的根节点
2️⃣根节点非空,则找到待插入的位置

在这里插入图片描述

public class Solution {
    /\*\*
 \* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 \*
 \* 
 \* @param root TreeNode类 
 \* @param val int整型 
 \* @return TreeNode类
 \*/
    public TreeNode insertToBST (TreeNode root, int val) {
        // write code here
        if(root==null) return new TreeNode(val);
        TreeNode curr=root;
        TreeNode parent=null;
        while(curr!=null){
            if(curr.val==val){
                break;
            }else if(curr.val<val){
                parent=curr;
                curr=curr.right;
            }else{
                parent=curr;
                curr=curr.left;
            }
        }
        if(curr==parent.left){
            parent.left=new TreeNode(val);
        }else{
            parent.right=new TreeNode(val);
        }
         
        return root;
    }
}

🍊4.删除二叉搜索树中的一个节点(较复杂)

NC297 删除一个二叉搜索树中的节点
在这里插入图片描述

public class Solution {
    /\*\*
 \* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 \*
 \* 
 \* @param root TreeNode类 
 \* @param key int整型 
 \* @return TreeNode类
 \*/
     public TreeNode deleteNode(TreeNode root, int key) {
        root = delete(root,key);
        return root;
    }

    private TreeNode delete(TreeNode root, int key) {
        if (root == null) return null;

        if (root.val > key) {
            root.left = delete(root.left,key);
        } else if (root.val < key) {
            root.right = delete(root.right,key);
        } else {
            if (root.left == null) return root.right;
            if (root.right == null) return root.left;
            TreeNode tmp = root.right;
            while (tmp.left != null) {
                tmp = tmp.left;
            }
            root.val = tmp.val;
            root.right = delete(root.right,tmp.val);
        }
        return root;
    }
}

🍊5.判断是不是二叉搜索树(易错)

NC184 判断是不是二叉搜索树

判断是不是二叉搜索树,就是看这棵二叉树是否满足二叉搜索树的性质,当二叉搜索树进行中序遍历的时候,它们的节点的值是按照从小到大排序的,我们可以利用这个性质来解答。在这里插入图片描述

public class Solution {
    /\*\*
 \* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 \*
 \* 


![img](https://img-blog.csdnimg.cn/img_convert/92e6149502d233a04c7a5811e397078a.png)
![img](https://img-blog.csdnimg.cn/img_convert/67fb327b1142f4e107e4cdd64d1ec8b6.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值