主题:面经
小结:
题目:
Check whether a binary tree is BST(if the binary tree is very large, you can not simply in-order print all the nodes out.)
Code:
class Solution{
public boolean isBST(Node root, Node min, Node max){
if(root == null) return true;
if(root.compareTo(min)<0 || root.compareTo(max)>0) return false;
return (isBST(root.left, min, root)) && (isBST(root.right, root, max));
}
}
小结:
一大一小两个标记,随着遍历树的过程变化。复杂度O(n),无多余空间。