public boolean isBST(BinaryTreeNode<Integer> root) { if (root == null) return true; boolean leftValid = root.getLeft() == null ? true : max(root.getLeft()) <= root.getValue(); boolean rightValid = root.getRight() == null ? true : max(root.getRight()) > root.getValue(); if (leftValid && rightValid) { return isBST(root.getLeft()) && isBST(root.getRight()); } return false; }
public Integer max(BinaryTreeNode<Integer> node) { while (node.getRight() != null) { node = node.getRight(); } return node.getValue(); }