当前位置:网站首页>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);
}
}
}

边栏推荐
- [difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
- leetcode刷题:二叉树24(二叉树的最近公共祖先)
- 2022聚合工艺考试题模拟考试题库及在线模拟考试
- 2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
- 爱可可AI前沿推介(7.7)
- visual stdio 2017关于opencv4.1的环境配置
- 【从 0 开始学微服务】【00】课程概述
- Cookie
- RHSA first day operation
- MPLS experiment
猜你喜欢

SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels

On valuation model (II): PE index II - PE band

金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)

SQL Lab (46~53) (continuous update later) order by injection

Charles: four ways to modify the input parameters or return results of the interface

How to apply @transactional transaction annotation to perfection?

【PyTorch实战】图像描述——让神经网络看图讲故事

普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
![Routing strategy of multi-point republication [Huawei]](/img/5c/2e3b739ce7199f0d2a4ddd7c3856fc.jpg)
Routing strategy of multi-point republication [Huawei]

SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
随机推荐
[learn microservice from 0] [01] what is microservice
Common knowledge of one-dimensional array and two-dimensional array
Static routing assignment of network reachable and telent connections
【从 0 开始学微服务】【03】初探微服务架构
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
通讯协议设计与实现
Day-19 IO stream
数据库安全的重要性
Object. Simple implementation of assign()
GCC compilation error
leetcode刷题:二叉树21(验证二叉搜索树)
layer弹出层的关闭问题
Four functions of opencv
Attack and defense world ----- summary of web knowledge points
ICLR 2022 | pre training language model based on anti self attention mechanism
IPv6 experiment
visual stdio 2017关于opencv4.1的环境配置
Polymorphism, final, etc
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
什么是ESP/MSR 分区,如何建立ESP/MSR 分区