利用中序遍历,传入一个参数,记录上一个结点,如果上一个结点比当前结点的值大,则不是二叉搜索树
- public boolean isBinarySearchTree(BinaryTreeNode node, BinaryTreeNode pre){
- if(node== null) return false;
- boolean leftSearch = true;
- boolean rightSearch = true;
- if(node.left!= null){
- leftSearch = isBinarySearchTree(node.left, pre);
- }
- if(pre.value> node.value) return false;
- pre.value = node.value;
- if(node.right!= null){
- rightSearch = isBinarySearchTree(node.right, pre);
- }
- return leftSearch && rightSearch;
- }
转载于:https://blog.51cto.com/fivedong/1007666