当前位置:网站首页>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;
}
}
边栏推荐
- NVIDIA可编程推理加速器TensorRT学习笔记(三)——加速推理
- Binary tree - 112. Path sum
- 网站服务器停止响应是什么意思?
- 抽丝剥茧C语言(高阶)程序环境和预处理
- 二叉树——104. 二叉树的最大深度
- 牛客/洛谷——[NOIP2003 普及组]栈
- Iterator pattern of behavioral pattern
- String functions and memory operation functions
- What is multithreading
- Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
猜你喜欢

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

BOM 浏览器对象模型

Article 75: writing skills of academic papers

LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果

二叉树——257. 二叉树的所有路径

Responsibility chain model of behavioral model

回溯——17. 电话号码的字母组合

“群魔乱舞”,牛市是不是结束了?2021-05-13

Basic syntax of MySQL DDL, DML and DQL

Find the cause of program dead cycle through debugging
随机推荐
The difference between SFTP and FTP
Leetcode question brushing series -- 931. Minimum sum of descent path
模块二作业
Key and difficult points of C language pointer
[ManageEngine] servicedesk plus won the 2022 safety model engineering data safety award
Stm32- analyze latency based on assembly
抽丝剥茧C语言(高阶)程序环境和预处理
What is multithreading
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
Seventy second: pedestrian detection
Binary tree -- 111. Minimum depth of binary tree
Pyqt5 rapid development and actual combat.pdf sharing
Lua脚本编写Wireshark插件解析第三方私有协议
Leetcode shahutong series -- 63. Different paths II
Sort fake contacts
Typescript TS basic knowledge and so on
QT smart pointer error prone point
Firewall command simple operation
二叉树——404. 左叶子之和
程序环境和预处理