当前位置:网站首页>Leetcode problem solving -- 98 Validate binary search tree
Leetcode problem solving -- 98 Validate binary search tree
2022-07-06 03:07:00 【Snowy solitary boat】
Recursive version
public boolean isValidBST(TreeNode root) {
return range(root,Integer.MIN_VALUE-1L,Integer.MAX_VALUE+1L);
}
public boolean range(TreeNode node,long min,long max){
if (node==null) return true;
if (node.val<=min||node.val>=max) return false;
return range(node.left,min,node.val)&&range(node.right,node.val,max);
}
Wrong thinking :
Before saying the right way of thinking , Let me first talk about the pit I stepped on , I was thinking about recursion , Then judge whether the value of the left node is greater than or equal to the root node or the value of the right node is less than or equal to the root node , Just go back to false, But in fact, this is not possible , Please look at the chart below.
7 / \ 2 5 / \ 1 9
According to the above idea, the result is true, This is because the upper limit of the left subtree is ignored , And the right subtree has the error caused by the lower limit
The right way of thinking :
Use the upper and lower limits to judge
- Because the root node has no upper and lower limits , Therefore, the maximum and minimum values that can be stored by the variable type are used as the upper and lower limits
- If the node is null Go straight back to true
- If the node is not null, And the node value is not within the range specified by the upper and lower limits ,return false
- Continue to judge subtree , But should pay attention to , The upper limit of the left subtree is the value of the current subtree , The lower limit of the right subtree is the value of the current subtree
Iteration version
public boolean isValidBST(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
long prev = Integer.MIN_VALUE-1L;
while (!stack.isEmpty()||node!=null){
while (node!=null){
stack.push(node);
node = node.left;
}
node = stack.pop();
if (node.val<=prev) return false;
prev = node.val;
node = node.right;
}
return true;
}
Ideas :
BSTree In the middle order traversal of , The value of the previous node to be traversed must be less than the value of the next node to be traversed , Therefore, a node that stores the value of the previous traversed node is added prev Variables for comparison
This scheme is based on the understanding of the iterative method of middle order traversal , If a little partner doesn't know , And I want to know more about the explanation idea of middle order traversal iteration , Sure Click here to jump
边栏推荐
- 原型图设计
- My C language learning record (blue bridge) -- on the pointer
- Introduction to robotframework (III) Baidu search of webui automation
- tcpdump: no suitable device found
- 深度解析链动2+1模式,颠覆传统卖货思维?
- These are not very good
- 建模规范:命名规范
- 深入探究指针及指针类型
- NR modulation 1
- I sorted out a classic interview question for my job hopping friends
猜你喜欢

Modeling specifications: naming conventions

深度解析链动2+1模式,颠覆传统卖货思维?

【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用

RobotFramework入门(三)WebUI自动化之百度搜索

【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service

Taobao focus map layout practice

CobaltStrike-4.4-K8修改版安装使用教程

Eight super classic pointer interview questions (3000 words in detail)
How to do function test well
如何做好功能测试
随机推荐
CobaltStrike-4.4-K8修改版安装使用教程
纯Qt版中国象棋:实现双人对战、人机对战及网络对战
Problems encountered in 2022 work IV
Summary of Bible story reading
NR modulation 1
全国大学生信息安全赛创新实践赛初赛---misc(永恒的夜)
Taobao focus map layout practice
4. File modification
Buuctf question brushing notes - [geek challenge 2019] easysql 1
Web security SQL injection vulnerability (1)
[network security interview question] - how to penetrate the test file directory through
Rust language -- iterators and closures
Polymorphic day02
Redis SDS principle
MySQL advanced notes
PMP每日一练 | 考试不迷路-7.5
How does yyds dry inventory deal with repeated messages in the consumption process?
如何做好功能测试
Is there a completely independent localization database technology
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]