当前位置:网站首页>98. Verify binary search tree (medium binary search tree DFS)
98. Verify binary search tree (medium binary search tree DFS)
2022-07-28 22:25:00 【Calm in the wind and rain】
98. Verify binary search tree
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 .
Example 1:

Input :root = [2,1,3]
Output :true
Example 2:

Input :root = [5,1,4,null,null,3,6]
Output :false
explain : The value of the root node is 5 , But the value of the right child node is 4 .
Tips :
The number of nodes in the tree ranges from [1, 104] Inside
-231 <= Node.val <= 231 - 1
source : Power button (LeetCode)
link :https://leetcode.cn/problems/validate-binary-search-tree
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
Need to understand the properties of binary search tree : If the left subtree of the binary tree is not empty , Then the values of all nodes on the left subtree are less than the values of its root nodes ; If its right subtree is not empty , Then the value of all nodes on the right subtree is greater than the value of its root node ; Its left and right subtrees are also binary search trees .
We can design a recursive function helper(root, lower, upper) To judge recursively , Function means to consider with root Root subtree , Determine whether the values of all nodes in the subtree are in (lower, upper) Within the scope of . If root The value of the node val be not in (lower, upper) If the condition is not satisfied, return directly , Otherwise, we need to continue to call recursively to check whether the left and right subtrees satisfy , If they are satisfied, it means that this is a binary search tree .
So according to the properties of the binary search tree , When the left subtree is called recursively , We need to put the upper bound upper Change it to root.val, That is to call helper(root.left, lower, root.val), Because the value of all nodes in the left subtree is less than the value of its root node . Similarly, when the right subtree is called recursively , We need to put the lower bound lower Change it to root.val, That is to call helper(root.right, root.val, upper).
Answer key (Java)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public boolean isValidBST(TreeNode root) {
return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean dfs(TreeNode root, long lower, long upper) {
if (root == null) {
return true;
}
if (root.val <= lower || root.val >= upper) {
return false;
}
return dfs(root.left, lower, root.val) && dfs(root.right, root.val, upper);
}
}
边栏推荐
- The person in charge of Tencent cloud database borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
- Necessary for in-depth learning: split the data set, split the labels according to the split pictures, and check the interval of all marked labels
- [Ruiji takeout] day05 package management business development
- HCIP(9)
- 乌官员:乌克兰一半农产品经多瑙河港口出口
- Learning notes and summary of C language programming specification
- Clearing of applet component timer
- Future trend of defi in bear market
- Learn kotlin - extension function
- 成立不到一年!MIT衍生量子计算公司完成900万美元融资
猜你喜欢

【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络
![[CS231N]Lecture_ 2:Image Classification pipelin](/img/4f/de56b071560ada746c587a9dbc5f02.jpg)
[CS231N]Lecture_ 2:Image Classification pipelin

HCIP(8)

SQL injection less42 (post stack injection)

Hcip seventh experiment

HCIP(13)

JS DOM编程之平平无奇小练习

Data visualization news, different forms of news reports

Apifox: satisfy all your fantasies about API

Desai wisdom number - line chart (stacking area chart): ranking of deposits of different occupational groups in the proportion of monthly income in 2022
随机推荐
【机器学习】朴素贝叶斯对文本分类--对人名国别分类
The difference between get and post
Lvs+keepalived high availability deployment practical application
Data visualization news, different forms of news reports
Use webworker to perform background tasks
Have you seen the management area decoupling architecture? Can help customers solve big problems
Ordinary practice of JS DOM programming
深度学习必备:对数据集的拆分、根据拆分图片拆分labels、对全部标注标签进行区间检查
Embrace open source guidelines
Static route and default route experiment
mysql create语句能不能用来建立表结构并追加新的记录
罗克韦尔AB PLC RSLogix数字量IO模块基本介绍
Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)
How to establish a decentralized community in Web3
Ruiji takeout project - development of business development function Day2
TensorFlow Serving 高性能的机器学习模型服务系统
HYDAC overflow valve db08a-01-c-n-500v
[CS231N]Lecture_ 2:Image Classification pipelin
静态成员static详解
Tensorflow serving high performance machine learning model service system