-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCheckBST.java
More file actions
40 lines (37 loc) · 1.22 KB
/
Copy pathCheckBST.java
File metadata and controls
40 lines (37 loc) · 1.22 KB
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
class BinaryTree {
static class Node {
int value;
Node left;
Node right;
Node(int value) {
this.value = value;
}
}
public static boolean isValidBST(Node root) {
return isValidBST(root, null, null);
}
public static boolean isValidBST(Node root, Integer max, Integer min) {
if (root == null) return true;
if (max != null && root.value >= max) return false;
if (min != null && root.value <= min ) return false;
return isValidBST(root.left, root.value, min) &&
isValidBST(root.right, max, root.value);
}
public static void main( String args[] ) {
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(2);
root.left.left.left = new Node(1);
root.left.right = new Node(7);
root.right.left = new Node(13);
root.right.left.right = new Node(14);
root.right.right = new Node(21);
if (BinaryTree.isValidBST(root))
System.out.println("A valid BST");
else
System.out.println("Not a valid BST");
}
}
//Time Complexity::O(N)
//Space Complexity::O(N)