当前位置:网站首页>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);
}
}
边栏推荐
- SQL注入 Less34(POST型宽字节注入+布尔盲注)
- Ordinary practice of JS DOM programming
- TensorFlow Serving 高性能的机器学习模型服务系统
- 记录Flutter解决A RenderFlex overflowed by 7.3 pixels on the bottom溢出问题
- 使用webWorker执行后台任务
- SSH password free login
- Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
- Alibaba cloud CDN practice
- 普源示波器实际的使用效果怎么样
- 使用百度EasyDL实现明厨亮灶厨师帽识别
猜你喜欢

LCR测试仪最为主要的功能和用途都是什么

HCIP(15)

HCIP(8)

拥抱开源指南

记录Flutter解决A RenderFlex overflowed by 7.3 pixels on the bottom溢出问题

Aimbetter insight into your database, DPM and APM solutions

HCIP(12)

HCIP(14)

Have you seen the management area decoupling architecture? Can help customers solve big problems

Win11 how to open software notification
随机推荐
Part 8: creating camera classes
Tensorflow serving high performance machine learning model service system
SQL injection less38 (Stack Injection)
[leetcode] maximum depth of binary tree
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login
C语言编程规范学习笔记和总结
Establishment of Ruiji takeout development environment
阿里云CDN实践
记录Flutter解决A RenderFlex overflowed by 7.3 pixels on the bottom溢出问题
Hcip experiment (15)
How to establish a decentralized community in Web3
Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
HCIP(11)
TensorFlow Serving 高性能的机器学习模型服务系统
vuejs中如何实现动态路由切换及路由的缓存
[CS231N]Lecture_ 2:Image Classification pipelin
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
LVS+KeepAlived高可用部署实战应用
Form validation and cascading drop-down lists (multiple implementations)
Summary of the use of hash table set and map when leetcode brushes questions