当前位置:网站首页>Search for an element in a binary search tree (BST)
Search for an element in a binary search tree (BST)
2022-07-07 08:05:00 【Hydrion-Qlz】
700. Search in binary search tree
The difficulty is simple
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 .
Ideas
For binary search trees (BST) Come on
- The value of the left child node is less than the value of the current node
- The value of the right child node is greater than the value of the current node
Therefore, for a specific node, we only need to judge the size relationship between the current node and the target value , Then continue to traverse
Does it look like a dichotomy ? Actually BST The middle order traversal of is an ascending array !
If you don't understand, please remember this conclusion , This is in the solution part and BST Relevant questions are very useful , for example
package cn.edu.xjtu.carlWay.tree.searchInBinarySearchTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 700. Search in binary search tree * Given a binary search tree (BST) The root node root And an integer value val. * <p> * 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 . * <p> * https://leetcode-cn.com/problems/search-in-a-binary-search-tree/ */
public class Solution {
public TreeNode searchBST(TreeNode root, int val) {
TreeNode node = root;
while (node != null) {
if (node.val == val) {
return node;
} else if (node.val > val) {
node = node.left;
} else {
node = node.right;
}
}
return null;
}
}
边栏推荐
- Wechat applet data binding multiple data
- Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
- Qt学习27 应用程序中的主窗口
- 探索干货篇!Apifox 建设思路
- Téléchargement des données de conception des puces
- [UVM foundation] what is transaction
- Linux server development, redis protocol and asynchronous mode
- Use and analysis of dot function in numpy
- Recursive method to verify whether a tree is a binary search tree (BST)
- PHP exports millions of data
猜你喜欢

Use and analysis of dot function in numpy

Sign up now | oar hacker marathon phase III, waiting for your challenge

You Li takes you to talk about C language 6 (common keywords)

2022年茶艺师(中级)考试试题及模拟考试

Cnopendata list data of Chinese colleges and Universities

Linux server development, MySQL transaction principle analysis

Niu Mei's mathematical problem --- combinatorial number
![[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)](/img/39/cac2b5492d374da393569e2ab467a4.png)
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)

追风赶月莫停留,平芜尽处是春山

Linux server development, redis protocol and asynchronous mode
随机推荐
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
Most elements
PHP exports millions of data
Leetcode 90: subset II
Recursive method constructs binary tree from middle order and post order traversal sequence
Chip design data download
芯片 设计资料下载
Rust Versus Go(哪种是我的首选语言?)
What is the interval in gatk4??
这5个摸鱼神器太火了!程序员:知道了快删!
青龙面板--花花阅读
Thinkcmf6.0安装教程
Cnopendata American Golden Globe Award winning data
mysql多列索引(组合索引)特点和使用场景
json 数据展平pd.json_normalize
Figure out the working principle of gpt3
Force buckle 144 Preorder traversal of binary tree
Roulette chart 2 - writing of roulette chart code
[UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
Linux server development, redis source code storage principle and data model