请找到下面的代码进行简单的二叉搜索树检查:
class Tree {
int value;
Tree left;
Tree right;
public Tree (int a){
value = a;
left = right = null;
}
}
public class VerifyBST {
public static boolean ifBST(Tree myTree, int small , int large){
if(myTree == null)
return true;
if(myTree.value > small && myTree.value < large){
boolean leftBST = ifBST(myTree.left, small,myTree.value);
boolean rightBST = ifBST(myTree.right,myTree.value,large);
return leftBST&&rightBST;
}
else{
return false;
}
}
public static void main(String[] args) {
/*
4
/ \
2 6
/ \ /\
1 3 5 7 */
Tree myTree = new Tree(4);
myTree.left = new Tree(2);
myTree.right = new Tree(6);
myTree.left.left = new Tree(1);
myTree.left.right = new Tree(3);
myTree.right.left = new Tree(5);
myTree.right.right = new Tree(7);
System.out.println("BST or NOT?" + ifBST(myTree,Integer.MIN_VALUE,Integer.MAX_VALUE));
}
}
我的问题:
从代码中可以清楚地看出,我已经手动输入了二叉树的所有条目,所以如果在某些情况下我需要检查大型树而手动输入不是一个好主意,那么最好的方法应该是什么应该遵循呢?
既然我已经传入
ifBST(myTree,Integer.MIN_VALUE,Integer.MAX_VALUE)
了main方法,这是否意味着Integer.MIN_VALUE = 1
并且Integer.MAX_VALUE = 7
被传递给了方法体?
谢谢