当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

Fixed and alternate sequential execution of modes

C language actual combat guessing game

Dead letter queue and message TTL expiration code

Leetcode200 - find detailed explanation of the number of islands

The GUI interface of yolov3 (simple, image detection)

C - readonly and const keywords

Are you still using your browser's own bookmarks? This bookmark plugin is awesome

Kubernetes网络插件详解 - Calico篇 - 概述

二叉树——112. 路径总和

Detailed explanation of kubernetes network plug-ins - calico chapter - Overview
随机推荐
Exercise (1) create a set C1 to store the elements "one", "two", "three"“
Unity—欧拉角,四元数
SIGIR‘22 推荐系统论文之图网络篇
ShardingSphere数据分片
二叉树——404. 左叶子之和
MySQL的DDL、DML和DQL的基本语法
Article 75: writing skills of academic papers
Practical experience of pair programming
Cherish time and improve efficiency
The difference between SFTP and FTP
Annotation @autowired source code analysis
GUI interface of yolov3 (2) -- beautify the page + output the name and quantity of the identified object
SHIB(柴犬币)一月涨幅数百倍,百倍币需具备哪些核心要素?2021-05-09
SQLZOO——Nobel Quiz
06_ue4进阶_使用地形工具设置大地图
What is parity? How to use C language?
The GUI interface of yolov3 (simple, image detection)
程序环境和预处理
Bubble sort idea and Implementation
Leetcode shahutong series -- 63. Different paths II