当前位置:网站首页>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;
}
}
边栏推荐
- game攻防世界逆向
- Leetcode 40: combined sum II
- 贝叶斯定律
- Use and analysis of dot function in numpy
- Ansible
- 【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
- You Li takes you to talk about C language 6 (common keywords)
- [advanced digital IC Verification] command query method and common command interpretation of VCs tool
- Shell 脚本的替换功能实现
- C语言通信行程卡后台系统
猜你喜欢
追风赶月莫停留,平芜尽处是春山
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
Problem solving: unable to connect to redis
padavan手动安装php
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
运放电路的反馈电阻上并联一个电容是什么作用
Linux server development, redis source code storage principle and data model
Ansible
Linux server development, SQL statements, indexes, views, stored procedures, triggers
json 数据展平pd.json_normalize
随机推荐
Chip design data download
Shell 脚本的替换功能实现
Bugku CTF daily one question chessboard with only black chess
padavan手动安装php
C语言航班订票系统
QT learning 26 integrated example of layout management
Roulette chart 2 - writing of roulette chart code
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
Qt学习28 主窗口中的工具栏
2022年茶艺师(中级)考试试题及模拟考试
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
Why should we understand the trend of spot gold?
Zhilian + AV, AITO asked M7 to do more than ideal one
青龙面板-今日头条
Qt学习26 布局管理综合实例
Lattice coloring - matrix fast power optimized shape pressure DP
Binary tree and heap building in C language
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
Numbers that appear only once
Visualization Document Feb 12 16:42