简单介绍下二叉树排序树:
- The value of every node in a node’s left subtree is less than the data value of that node.
The value of every node in a node’s right subtree is greater than the data value of that node.
之前在网上看到很多人写的,只想说你们测试过吗?你们测试了几个案例?
下面开始写我在hackerrank测试满分的源码:
boolean checkBST(Node root) {
return checkBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
boolean checkBST(Node root, int min, int max) {
if (root == null) return true;
if (root.data <= min || root.data >= max)
return false;
if (!checkBST(root.left, min, root.data) || !checkBST(root.right, root.data, max))
return false;
return true;
}
重载是为了支持更多方式使用,根本还是第二个方法。