大数据最全数据结构与算法 二叉搜索树_层次化结构从小到大排序node grid,2024年华为大数据开发面试真题解析

img
img

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

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

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

🔥网站推荐:千里之行,始于足下。每天坚持刷题,巩固所学知识,也为将来找工作,面试做好准备-----
牛客网刷题神器

🍊1. 二叉搜索树的概念

在这里插入图片描述

🎈二叉搜索树又叫做二叉排序树,它具有下面的几个性质:
1️⃣若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
2️⃣若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
3️⃣它的左、右子树也分别为二叉排序树

🍊2.二叉搜索树中的查找

给定一棵二叉搜索树,以及一个值val,如果二叉搜索树中存在值和val相同的节点,则返回这个节点,否则返回null
在这里插入图片描述

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;


![img](https://img-blog.csdnimg.cn/img_convert/a3f097fc2c4a4976aa8eca66fa72be53.png)
![img](https://img-blog.csdnimg.cn/img_convert/836484f5a0eb7e39ba701ba3e6be8e71.png)

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

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


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

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


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值