| 33 | /* Returns true if the given tree is a BST and its |
| 34 | values are >= min and <= max. */ |
| 35 | boolean isBSTUtil(Node node, int min, int max) |
| 36 | { |
| 37 | /* an empty tree is BST */ |
| 38 | if (node == null) |
| 39 | return true; |
| 40 | |
| 41 | /* false if this node violates the min/max constraints */ |
| 42 | if (node.data < min || node.data > max) |
| 43 | return false; |
| 44 | |
| 45 | /* otherwise check the subtrees recursively |
| 46 | tightening the min/max constraints */ |
| 47 | // Allow only distinct values |
| 48 | return (isBSTUtil(node.left, min, node.data-1) && |
| 49 | isBSTUtil(node.right, node.data+1, max)); |
| 50 | } |
| 51 | |
| 52 | /* Driver program to test above functions */ |
| 53 | public static void main(String args[]) |