当前位置:网站首页>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);
}
}
}
边栏推荐
- xshell评估期已过怎么办
- 通讯协议设计与实现
- 爱可可AI前沿推介(7.7)
- Cryptography series: detailed explanation of online certificate status protocol OCSP
- 静态Vxlan 配置
- "Series after reading" my God! It's so simple to understand throttling and anti shake~
- In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
- [疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
- leetcode刷题:二叉树23(二叉搜索树中的众数)
- Airserver automatically receives multi screen projection or cross device projection
猜你喜欢
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
BGP third experiment report
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
图形对象的创建与赋值
随机推荐
SQL head injection -- injection principle and essence
On valuation model (II): PE index II - PE band
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
Day-16 set
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
BGP actual network configuration
编译 libssl 报错
【PyTorch实战】图像描述——让神经网络看图讲故事
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
图形对象的创建与赋值
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
图像像素读写操作
What if does not match your user account appears when submitting the code?
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
idm服务器响应显示您没有权限下载解决教程
ICLR 2022 | pre training language model based on anti self attention mechanism
Polymorphism, final, etc