- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST_validate.java
More file actions
Latest commit
40 lines (36 loc) · 1016 Bytes
/
Copy pathBST_validate.java
File metadata and controls
40 lines (36 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Link : https://leetcode.com/problems/validate-binary-search-tree/
/**
* 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;
* }
* }
*/
classSolution {
publicbooleanisValidBST(TreeNoderoot) {
returndummy(root,Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITY);
}
publicbooleandummy(TreeNodep,doublemin,doublemax){
if(p==null){
returntrue;
}
if((p.val<=min)||(p.val>=max))
{
returnfalse;
}
booleanleftBST = dummy(p.left,min,p.val);
booleanrightBST = dummy(p.right,p.val,max);
if((leftBST&&rightBST)==false){
returnfalse;
}
returntrue;
}
}