当前位置:网站首页>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);
}
}
}
边栏推荐
- SQL head injection -- injection principle and essence
- 广州市召开安全生产工作会议
- Cryptography series: detailed explanation of online certificate status protocol OCSP
- 基于NeRF的三维内容生成
- How to apply @transactional transaction annotation to perfection?
- SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
- Attack and defense world - PWN learning notes
- Day-15 common APIs and exception mechanisms
- leetcode刷题:二叉树23(二叉搜索树中的众数)
- 【统计学习方法】学习笔记——支持向量机(上)
猜你喜欢
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
【PyTorch实战】用RNN写诗
OSPF exercise Report
【统计学习方法】学习笔记——第五章:决策树
【统计学习方法】学习笔记——支持向量机(上)
[statistical learning method] learning notes - logistic regression and maximum entropy model
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
Common knowledge of one-dimensional array and two-dimensional array
随机推荐
[statistical learning method] learning notes - support vector machine (Part 2)
The IDM server response shows that you do not have permission to download the solution tutorial
密码学系列之:在线证书状态协议OCSP详解
How to apply @transactional transaction annotation to perfection?
leetcode刷题:二叉树23(二叉搜索树中的众数)
File upload vulnerability - upload labs (1~2)
2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
有什么类方法或是函数可以查看某个项目的Laravel版本的?
How to use PS link layer and shortcut keys, and how to do PS layer link
【PyTorch实战】图像描述——让神经网络看图讲故事
GCC compilation error
聊聊Redis缓存4种集群方案、及优缺点对比
idm服务器响应显示您没有权限下载解决教程
3D content generation based on nerf
Vxlan 静态集中网关
Cryptography series: detailed explanation of online certificate status protocol OCSP
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
[爬虫]使用selenium时,躲避脚本检测
ip2long之后有什么好处?
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型