当前位置:网站首页>Binary tree -- 700. Search in binary search tree
Binary tree -- 700. Search in binary search tree
2022-07-26 00:05:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given a binary search tree (BST) The root node root And an integer value val.
You need to BST The node value found in is equal to val The node of . Return the subtree with this node as the root . If the node doesn't exist , Then return to null .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/search-in-a-binary-search-tree
2 Title Example


3 Topic tips
The number of nodes in the number is [1, 5000] Within the scope of
1 <= Node.val <= 10^7
root It's a binary search tree
1 <= val <= 10^7
4 Ideas
Method 1 : recursive
The binary search tree satisfies the following properties :
- The element values of all nodes in the left subtree are less than those of the root ;
- The element value of all nodes in the right subtree is greater than that of the root .
The following algorithm can be obtained : - if root If it is empty, an empty node will be returned ;
- if val = root.val, Then return to root;
- if val < root.val, Recursive left subtree ;
- if val > root.val, Recursive right subtree .
Complexity analysis
- Time complexity :O(N), among N Is the number of nodes in the binary search tree . In the worst case, the binary search tree is — Chain , And the element to be found is smaller than the element value at the end of the chain ( Big ), In this case, we need recursion N Time
- Spatial complexity :O(N). In the worst case, recursion requires O(N) Stack space of .
Method 2 : iteration
We're going to approach — The recursion of is changed into iterative writing :
- if root If it is empty, it will jump out of the cycle , And return an empty node ;
- if val= root.val, Then return to root;
- if val <root.val, take root Set as root.left;
- if val > root.val, take root Set as root.right.
Complexity analysis
- Time complexity :O(N), among N Is the number of nodes in the binary search tree . In the worst case, the binary search tree is — Chain , And the element to be found is smaller than the element value at the end of the chain ( Big ), In this case, we need to iterate Ⅳ Time
- Spatial complexity :O(1). No use of extra space .
5 My answer
recursive :
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) {
return null;
}
if (val == root.val) {
return root;
}
return searchBST(val < root.val ? root.left : root.right, val);
}
}
iteration :
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while (root != null) {
if (val == root.val) {
return root;
}
root = val < root.val ? root.left : root.right;
}
return null;
}
}
边栏推荐
猜你喜欢

什么是奇偶校验?如何用C语言实现?

二叉树——222. 完全二叉树的节点个数

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
34-SparkSQL自定义函数的使用、SparkStreaming的架构及计算流程、DStream转换操作、SparkStreaming对接kafka和offset的处理

如何用yolov5 做个闯红灯监控的智能交通系统(1)

二叉树——112. 路径总和

SIGIR‘22 推荐系统论文之图网络篇

Stm32- analyze latency based on assembly

Module II operation

VSCode格式化Json文件
随机推荐
Demo of pointer function
Dead letter queue and message TTL expiration code
Annotation @autowired source code analysis
通货膨胀之下,后市如何操作?2021-05-14
C - readonly and const keywords
Unexpected dubug tricks
寻找链表的中间节点
Seventy second: pedestrian detection
QT smart pointer error prone point
STM32 timer
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
Stm32- analyze latency based on assembly
网站服务器停止响应是什么意思?
Binary tree -- 111. Minimum depth of binary tree
NVIDIA可编程推理加速器TensorRT学习笔记(三)——加速推理
@Autowired注解的底层原理
Getaverse,走向Web3的远方桥梁
Song of statistics lyrics
C language (high level) program environment and preprocessing
Getting started with Servlet