当前位置:网站首页>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
边栏推荐
- [kubernetes series] learn the exposed application of kubernetes service security
- 继承day01
- Fault analysis | analysis of an example of MySQL running out of host memory
- Master data management theory and Practice
- 如何精准识别主数据?
- 主数据管理(MDM)的成熟度
- CSP date calculation
- How to do function test well
- 主数据管理理论与实践
- What are the principles of software design (OCP)
猜你喜欢

XSS challenges绕过防护策略进行 XSS 注入

C # create self host webservice

Derivation of anti Park transform and anti Clarke transform formulas for motor control
![[pointer training - eight questions]](/img/fd/1aa3937548a04078c4d7e08198c3a8.png)
[pointer training - eight questions]

NR modulation 1

IPv6 jobs

Reverse repackaging of wechat applet
![[matlab] access of variables and files](/img/cf/6f3cfdc4310fcf0bdcaa776d68261e.jpg)
[matlab] access of variables and files

codeforces每日5题(均1700)-第六天

Codeworks 5 questions per day (1700 average) - day 6
随机推荐
C language - Blue Bridge Cup - promised score
Day 50 - install vsftpd on ceontos6.8
OCR文字识别方法综述
深入探究指针及指针类型
Descriptor implements ORM model
PMP practice once a day | don't get lost in the exam -7.5
QT release exe software and modify exe application icon
Selenium share
Codeworks 5 questions per day (1700 average) - day 6
Overview of OCR character recognition methods
有没有完全自主的国产化数据库技术
[unity3d] GUI control
Detailed use of dbutils # yyds dry goods inventory #
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
1. Dynamic parameters of function: *args, **kwargs
CSP date calculation
Technology sharing | what if Undo is too big
Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
[ruoyi] set theme style
Self made CA certificate and SSL certificate using OpenSSL