| 5 | public class Question { |
| 6 | public static Integer last_printed = null; |
| 7 | public static boolean checkBST(TreeNode n) { |
| 8 | if (n == null) { |
| 9 | return true; |
| 10 | } |
| 11 | |
| 12 | // Check / recurse left |
| 13 | if (!checkBST(n.left)) { |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | // Check current |
| 18 | if (last_printed != null && n.data <= last_printed) { |
| 19 | return false; |
| 20 | } |
| 21 | last_printed = n.data; |
| 22 | |
| 23 | // Check / recurse right |
| 24 | if (!checkBST(n.right)) { |
| 25 | return false; |
| 26 | } |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | public static void main(String[] args) { |
| 31 | int[] array = {Integer.MIN_VALUE, Integer.MAX_VALUE - 2, Integer.MAX_VALUE - 1, Integer.MAX_VALUE}; |