当前位置:网站首页>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);
}
}
}
边栏推荐
- Problem: the string and characters are typed successively, and the results conflict
- SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
- Experiment with a web server that configures its own content
- Error in compiling libssl
- Review and arrangement of HCIA
- Tutorial on principles and applications of database system (009) -- conceptual model and data model
- 【统计学习方法】学习笔记——提升方法
- Realize all, race, allsettled and any of the simple version of promise by yourself
- SQL head injection -- injection principle and essence
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
猜你喜欢
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
【统计学习方法】学习笔记——提升方法
About web content security policy directive some test cases specified through meta elements
Vxlan static centralized gateway
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
Attack and defense world - PWN learning notes
Simple network configuration for equipment management
随机推荐
[statistical learning methods] learning notes - improvement methods
File upload vulnerability - upload labs (1~2)
Introduction to three methods of anti red domain name generation
Simple network configuration for equipment management
Using stack to convert binary to decimal
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
EPP+DIS学习之路(1)——Hello world!
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
About web content security policy directive some test cases specified through meta elements
Problem: the string and characters are typed successively, and the results conflict
2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
Completion report of communication software development and Application
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
EPP+DIS学习之路(2)——Blink!闪烁!
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
Let digital manage inventory
【PyTorch实战】用RNN写诗
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
OSPF exercise Report