当前位置:网站首页>Leetcode skimming: binary tree 20 (search in binary search tree)
Leetcode skimming: binary tree 20 (search in binary search tree)
2022-07-07 12:47:00 【Taotao can't learn English】
700. Search in binary search tree
Given a binary search tree (BST) And a value . You need to BST Found a node whose value is equal to the given value . Return the subtree with this node as the root . If the node doesn't exist , Then return to NULL.
for example ,
In the example above , If the value is 5, But because no node has a value of 5, We should go back to NULL.
A very simple question . recursive , When the current node is empty , Returns an empty , The current node is larger than the target number , Check the left subtree , Otherwise, check the right subtree , If equal , Then return to . Because binary search tree is non decreasing . Orderly , The idea of dichotomy .
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. Search in binary search tree **/
public class SearchBST {
/** * BST, Also called balanced binary tree , It is a binary tree accessed by key , * And it is required to keep the order , That is, any node is not less than its left offspring , Not greater than its right offspring ( Note that offspring , It's not a child ). * BST The order of the ergodic sequence must be monotonic and non descending . * * @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) {
// The current value is smaller than the target value , Search on the right subtree
return searchBST(root.right, val);
} else {
// The current value is larger than the target value , Search on the left sub tree
return searchBST(root.left, val);
}
}
}
边栏推荐
- HZOJ #235. 递归实现指数型枚举
- Routing strategy of multi-point republication [Huawei]
- SQL lab 1~10 summary (subsequent continuous update)
- OSPF exercise Report
- Image pixel read / write operation
- Cookie
- 爱可可AI前沿推介(7.7)
- Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
- xshell评估期已过怎么办
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
猜你喜欢
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
【统计学习方法】学习笔记——提升方法
Customize the web service configuration file
Zhimei creative website exercise
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
爱可可AI前沿推介(7.7)
HZOJ #240. 图形打印四
Minimalist movie website
[statistical learning methods] learning notes - Chapter 5: Decision Tree
SQL Lab (46~53) (continuous update later) order by injection
随机推荐
图像像素读写操作
idm服务器响应显示您没有权限下载解决教程
GCC compilation error
密码学系列之:在线证书状态协议OCSP详解
聊聊Redis缓存4种集群方案、及优缺点对比
Sorting, dichotomy
How to apply @transactional transaction annotation to perfection?
[爬虫]使用selenium时,躲避脚本检测
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
Realize a simple version of array by yourself from
SQL head injection -- injection principle and essence
[learn microservices from 0] [03] explore the microservice architecture
Polymorphism, final, etc
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
【统计学习方法】学习笔记——支持向量机(下)
2022 polymerization process test question simulation test question bank and online simulation test
OSPF exercise Report
[pytorch practice] use pytorch to realize image style migration based on neural network