当前位置:网站首页>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;
}
}
边栏推荐
- Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
- Force buckle 145 Binary Tree Postorder Traversal
- [quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
- Hands on deep learning (IV) -- convolutional neural network CNN
- Leetcode 40: combined sum II
- Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
- Quickly use Jacobo code coverage statistics
- Force buckle 144 Preorder traversal of binary tree
- Yugu p1020 missile interception (binary search)
- Niu Mei's mathematical problem --- combinatorial number
猜你喜欢
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
The charm of SQL optimization! From 30248s to 0.001s
JSON data flattening pd json_ normalize
快速使用 Jacoco 代码覆盖率统计
You Li takes you to talk about C language 6 (common keywords)
Visualization Document Feb 12 16:42
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
青龙面板-今日头条
2022 welder (elementary) judgment questions and online simulation examination
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
随机推荐
The charm of SQL optimization! From 30248s to 0.001s
Summary of redis functions
[UVM foundation] what is transaction
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
Linux Installation MySQL 8.0 configuration
Linux server development, redis protocol and asynchronous mode
A bit of knowledge - about Apple Certified MFI
Quickly use Jacobo code coverage statistics
C语言航班订票系统
Main window in QT learning 27 application
2022制冷与空调设备运行操作复训题库及答案
Zsh shell adds automatic completion and syntax highlighting
这5个摸鱼神器太火了!程序员:知道了快删!
LeetCode 40:组合总和 II
buuctf misc USB
Lattice coloring - matrix fast power optimized shape pressure DP
Figure out the working principle of gpt3