当前位置:网站首页>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;
}
}
边栏推荐
- Recursive method to verify whether a tree is a binary search tree (BST)
- Roulette chart 2 - writing of roulette chart code
- 有 Docker 谁还在自己本地安装 Mysql ?
- [quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
- C语言通信行程卡后台系统
- [2022 actf] Web Topic recurrence
- Linux Installation MySQL 8.0 configuration
- dash plotly
- Codeforce c.strange test and acwing
- Qt学习26 布局管理综合实例
猜你喜欢
Quickly use Jacobo code coverage statistics
2022制冷与空调设备运行操作复训题库及答案
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Force buckle 144 Preorder traversal of binary tree
Wechat applet data binding multiple data
Common validation comments
快速使用 Jacoco 代码覆盖率统计
[2022 ciscn] replay of preliminary web topics
微信小程序基本组件使用介绍
Most elements
随机推荐
追风赶月莫停留,平芜尽处是春山
CTF daily question day43 rsa5
Pytorch(六) —— 模型调优tricks
Rust Versus Go(哪种是我的首选语言?)
LeetCode简单题之判断一个数的数字计数是否等于数位的值
Why should we understand the trend of spot gold?
这5个摸鱼神器太火了!程序员:知道了快删!
dash plotly
LeetCode 40:组合总和 II
Quickly use Jacobo code coverage statistics
让Livelink初始Pose与动捕演员一致
青龙面板-今日头条
[2022 ciscn] replay of preliminary web topics
Codeforces Global Round 19
C language flight booking system
Linux server development, MySQL process control statement
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
Ansible
A bit of knowledge - about Apple Certified MFI
Linux server development, redis source code storage principle and data model