当前位置:网站首页>Recursive method to verify whether a tree is a binary search tree (BST)
Recursive method to verify whether a tree is a binary search tree (BST)
2022-07-07 08:04:00 【Hydrion-Qlz】
98. Verify binary search tree
Medium difficulty 1407
Give you the root node of a binary tree root
, Determine whether it is an effective binary search tree .
It works The binary search tree is defined as follows :
- The left subtree of the node contains only Less than Number of current nodes .
- The right subtree of the node contains only Greater than Number of current nodes .
- All left and right subtrees themselves must also be binary search trees .
Ideas
Verify whether a tree is BST Trees , We must be right about BST The definition of is very clear
- Left subtree All nodes The value of is less than the value of the current node
- Right subtree All nodes The value of is greater than the value of the current node
Therefore, we need to keep the maximum and minimum values from the root node to the current node during recursive traversal
Because the title explains that the value of each node can be taken int All numbers in the range , Then when we specify the minimum value, we can only specify long The minimum value of a type , It could go wrong
So when solving problems, you need to pay attention to the given data range , If the given data range is very large, don't consider the violence law ,99% It's going to be overtime
package cn.edu.xjtu.carlWay.tree.validateBinarySearchTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 98. Verify binary search tree * Give you the root node of a binary tree root , Determine whether it is an effective binary search tree . * <p> * It works The binary search tree is defined as follows : * <p> * The left subtree of the node contains only Less than Number of current nodes . * The right subtree of the node contains only Greater than Number of current nodes . * All left and right subtrees themselves must also be binary search trees . * <p> * https://leetcode-cn.com/problems/validate-binary-search-tree/ */
public class Solution {
public boolean isValidBST(TreeNode root) {
return validBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean validBST(TreeNode root, long minValue, long maxValue) {
if (root == null) {
return true;
}
if (root.val < maxValue && root.val > minValue) {
return validBST(root.left, minValue, root.val) && validBST(root.right, root.val, maxValue);
}
return false;
}
}
边栏推荐
- Ansible
- [guess-ctf2019] fake compressed packets
- Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
- 【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
- Linux Installation MySQL 8.0 configuration
- Wechat applet data binding multiple data
- Introduction to basic components of wechat applet
- mysql多列索引(组合索引)特点和使用场景
- C language queue
- Zhilian + AV, AITO asked M7 to do more than ideal one
猜你喜欢
Common validation comments
Shell 脚本的替换功能实现
PHP exports millions of data
Linux server development, SQL statements, indexes, views, stored procedures, triggers
JS quick start (I)
2022焊工(初级)判断题及在线模拟考试
Most elements
Hands on deep learning (IV) -- convolutional neural network CNN
Qt学习28 主窗口中的工具栏
Explore Cassandra's decentralized distributed architecture
随机推荐
Use and analysis of dot function in numpy
2022 tea master (intermediate) examination questions and mock examination
The charm of SQL optimization! From 30248s to 0.001s
[CV] Wu Enda machine learning course notes | Chapter 8
芯片 設計資料下載
Linux Installation MySQL 8.0 configuration
C语言航班订票系统
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
Cnopendata list data of Chinese colleges and Universities
misc ez_ usb
Operation suggestions for today's spot Silver
[VHDL parallel statement execution]
快速使用 Jacoco 代码覆盖率统计
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
php导出百万数据
Force buckle 144 Preorder traversal of binary tree
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
Record a stroke skin bone error of the skirt
2022 welder (elementary) judgment questions and online simulation examination
padavan手动安装php