forked from Anjalijha12345/DSA--Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisValidBST.java
More file actions
44 lines (30 loc) · 684 Bytes
/
isValidBST.java
File metadata and controls
44 lines (30 loc) · 684 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
41
42
43
44
import java.util.*;
public class isValidBST {
static class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
}
}
public static boolean isValid(Node root , Node min , Node max){
if(root == null){
return true;
}
if(min!=null &&root.data <=min.data ){
return false;
}
if(max !=null && root.data >=max.data ){
return false;
}
return isValid(root.left,min,max) && isValid(root.right,root,max);
}
public static void main(String args[]) {
if(isValid(null ,null ,null)){
System.out.println("Valid");
}else{
System.out.println("Not Valid");
}
}
}