当前位置:网站首页>leetcode刷题:二叉树20(二叉搜索树中的搜索)
leetcode刷题:二叉树20(二叉搜索树中的搜索)
2022-07-07 10:30:00 【涛涛英语学不进去】
700.二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。
例如,
在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。
很简单的一题。递归,当前结点为空时,返回空,当前结点比目标数大,则查左子树,否则查右子树,若相等,则返回。因为二叉搜索树是中序非递减的。即有序的,二分法思想。
package com.programmercarl.tree;
/** * @ClassName SearchBST * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 21:17 * @Version 1.0 * https://leetcode.cn/problems/search-in-a-binary-search-tree/ * 700. 二叉搜索树中的搜索 **/
public class SearchBST {
/** * BST,又叫平衡二叉树,是一种循关键码访问的二叉树, * 并且要求保持顺序性,即任一节点不小于其左后代,不大于其右后代(注意是后代,不是孩子)。 * BST的顺序性使得其中序遍历序列一定是单调非降的。 * * @param root * @param val * @return */
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) {
return null;
}
if (root.val == val) {
return root;
} else if (root.val < val) {
//当前值比目标值小,在右子树上搜
return searchBST(root.right, val);
} else {
//当前值比目标值大,在左子树上搜
return searchBST(root.left, val);
}
}
}
边栏推荐
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- What are the technical differences in source code anti disclosure
- 【统计学习方法】学习笔记——提升方法
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
- Is it safe to open Huatai's account in kainiu in 2022?
- About sqli lab less-15 using or instead of and parsing
- @Bean与@Component用在同一个类上,会怎么样?
- 开发一个小程序商城需要多少钱?
- How to use PS link layer and shortcut keys, and how to do PS layer link
猜你喜欢
Zero shot, one shot and few shot
The IDM server response shows that you do not have permission to download the solution tutorial
【PyTorch实战】图像描述——让神经网络看图讲故事
盘点JS判断空对象的几大方法
PowerShell cs-utf-16le code goes online
RHSA first day operation
College entrance examination composition, high-frequency mention of science and Technology
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Static routing assignment of network reachable and telent connections
消息队列消息丢失和消息重复发送的处理策略
随机推荐
BGP actual network configuration
【统计学习方法】学习笔记——提升方法
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
NGUI-UILabel
Minimalist movie website
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
数据库系统原理与应用教程(008)—— 数据库相关概念练习题
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
静态Vxlan 配置
Tutorial on principles and applications of database system (007) -- related concepts of database
[statistical learning methods] learning notes - improvement methods
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
RHSA first day operation
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
消息队列消息丢失和消息重复发送的处理策略
Tutorial on principles and applications of database system (009) -- conceptual model and data model
What are the technical differences in source code anti disclosure
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
Object. Simple implementation of assign()