当前位置:网站首页>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;
}
}
边栏推荐
- Cnopendata American Golden Globe Award winning data
- 2022制冷与空调设备运行操作复训题库及答案
- Linux Installation MySQL 8.0 configuration
- 【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
- Pytorch parameter initialization
- Padavan manually installs PHP
- game攻防世界逆向
- [UVM foundation] what is transaction
- 2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
- Li Kou interview question 04.01 Path between nodes
猜你喜欢
LeetCode 90:子集 II
Linux server development, redis source code storage principle and data model
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
JSON data flattening pd json_ normalize
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
Quickly use Jacobo code coverage statistics
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
Qt学习28 主窗口中的工具栏
JS quick start (I)
Why should we understand the trend of spot gold?
随机推荐
芯片 設計資料下載
C language communication travel card background system
C语言二叉树与建堆
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
2022年茶艺师(中级)考试试题及模拟考试
Common validation comments
Who has docker to install MySQL locally?
青龙面板--整理能用脚本
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
Installing postgresql11 database under centos7
Value sequence (subsequence contribution problem)
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
[advanced digital IC Verification] command query method and common command interpretation of VCs tool
Linux server development, redis source code storage principle and data model
快速使用 Jacoco 代码覆盖率统计
自定义类加载器加载网络Class
Introduction to basic components of wechat applet
Zsh shell adds automatic completion and syntax highlighting
Chip design data download
运放电路的反馈电阻上并联一个电容是什么作用