forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidBST.java
More file actions
37 lines (30 loc) · 680 Bytes
/
ValidBST.java
File metadata and controls
37 lines (30 loc) · 680 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
package Trees;
/**
* Author - archit.s
* Date - 04/11/18
* Time - 4:16 PM
*/
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
left=null;
right=null;
}
}
public class ValidBST {
public boolean helper(TreeNode root, int MIN, int MAX){
if(root == null){
return true;
}
if(root.val > MIN && root.val < MAX){
return helper(root.left, MIN, root.val) && helper(root.right, root.val, MAX);
}
return false;
}
public int isValidBST(TreeNode A) {
return helper(A, Integer.MIN_VALUE, Integer.MAX_VALUE) ? 1:0;
}
}