Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package weblab;
- import java.util.*;
- class Solution {
- /**
- * Computes whether the BinaryTree is a binary search tree.
- *
- * @param tree
- * the BinaryTree to check.
- * @return true iff the BinaryTree is a binary search tree, else false.
- */
- public static boolean isTreeBST(BinaryTree tree) {
- return bst(tree, Integer.MIN_VALUE,
- Integer.MAX_VALUE);
- }
- public static boolean bst(BinaryTree tree, int min, int max){
- // TODO
- if(tree == null){
- return true;
- }
- if(tree.getKey() < min || tree.getKey() > max){
- return false;
- }
- return bst(tree.getLeft(), min, tree.getKey() - 1) && bst(tree.getRight(), tree.getKey()+1, max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement