-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJudgeBST.java
45 lines (38 loc) · 1.1 KB
/
JudgeBST.java
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
45
/**
* Created by Yuzhang on 1/30/17.
*/
public class JudgeBST {
static class TreeNode {
int data;
TreeNode left, right;
public TreeNode(int item) {
data = item;
left = right = null;
}
}
TreeNode root;
boolean isBST(TreeNode node) {
return isBST(node, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
boolean isBST(TreeNode node, int min, int max) {
if (node == null) {
return true;
}
if (node.data <= min || node.data >= max) {
return false;
}
return isBST(node.left, min, node.data) && isBST(node.right, node.data, max);
}
public static void main(String args[]) {
JudgeBST tree = new JudgeBST();
tree.root = new TreeNode(4);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(5);
tree.root.left.left = new TreeNode(1);
tree.root.left.right = new TreeNode(3);
if (tree.isBST(tree.root))
System.out.println("IS BST");
else
System.out.println("Not a BST");
}
}