当前位置:网站首页>二叉树——700.二叉搜索树中的搜索
二叉树——700.二叉搜索树中的搜索
2022-07-25 23:55:00 【向着百万年薪努力的小赵】
1 题目描述
给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/search-in-a-binary-search-tree
2 题目示例


3 题目提示
数中节点数在 [1, 5000] 范围内
1 <= Node.val <= 10^7
root 是二叉搜索树
1 <= val <= 10^7
4 思路
方法一:递归
二叉搜索树满足如下性质:
- 左子树所有节点的元素值均小于根的元素值;
- 右子树所有节点的元素值均大于根的元素值。
据此可以得到如下算法: - 若root为空则返回空节点;
- 若val = root.val,则返回root;
- 若val < root.val,递归左子树;
- 若val > root.val,递归右子树。
复杂度分析
- 时间复杂度:O(N),其中N是二叉搜索树的节点数。最坏情况下二叉搜索树是—条链,且要找的元素比链末尾的元素值还要小(大),这种情况下我们需要递归N次
- 空间复杂度:O(N)。最坏情况下递归需要O(N)的栈空间。
方法二:迭代
我们将方法—的递归改成迭代写法:
- 若root为空则跳出循环,并返回空节点;
- 若val= root.val,则返回root;
- 若val <root.val,将root置为root.left;
- 若val > root.val,将root置为root.right。
复杂度分析
- 时间复杂度:O(N),其中N是二叉搜索树的节点数。最坏情况下二叉搜索树是—条链,且要找的元素比链末尾的元素值还要小(大),这种情况下我们需要迭代Ⅳ次
- 空间复杂度:O(1)。没有使用额外的空间。
5 我的答案
递归:
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);
}
}
迭代:
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;
}
}
边栏推荐
- String functions and memory operation functions
- ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
- S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)
- [Database Foundation] summary of MySQL Foundation
- Bubble sort idea and Implementation
- Find the cause of program dead cycle through debugging
- 抽丝剥茧C语言(高阶)程序环境和预处理
- Key and difficult points of C language pointer
- Song of statistics lyrics
- [Muduo] EventLoop event cycle
猜你喜欢

Fixed and alternate sequential execution of modes

Part 66: monocular 3D reconstruction point cloud

Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms

Generating random number random learning uniform_ int_ distribution,uniform_ real_ distribution

Redis extended data type (jump table /bitmaps/hyperloglog/geospatial)

Find the cause of program dead cycle through debugging

redis-扩展数据类型(跳跃表/BitMaps/HyperLogLog/GeoSpatial)

TOPSIS and entropy weight method

redis-基本数据类型(String/list/Set/Hash/Zset)

ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
随机推荐
S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)
C language implementation of three chess
Storage of data in memory
Three board axe! Help you become an excellent software engineer
The late Apple co-founder Steve Jobs was posthumously awarded the U.S. presidential medal of freedom
程序员面试金典 4.12 求和路径
[JUC] concurrent keyword volatile
Quick sorting of top ten sorting
Programmer interview Golden Classic 4.12 summation path
Read the field status of account in ABAP code (hidden, optional, required)
Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data
String functions and memory operation functions
Array merge method: concat()
Unexpected dubug tricks
2022-07-18 study notes of group 5 self-cultivation class (every day)
Macro task, micro task and event cycle mechanism
Demo of pointer function
SIGIR‘22 推荐系统论文之图网络篇
Reduce method of array
Part 74: overview of machine learning optimization methods and superparameter settings