Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. package weblab;
  2.  
  3. import java.util.*;
  4.  
  5. class Solution {
  6.  
  7.   /**
  8.    * Computes whether the BinaryTree is a binary search tree.
  9.    *
  10.    * @param tree
  11.    *     the BinaryTree to check.
  12.    * @return true iff the BinaryTree is a binary search tree, else false.
  13.    */
  14.   public static boolean isTreeBST(BinaryTree tree) {
  15.     return bst(tree, Integer.MIN_VALUE,
  16.                                Integer.MAX_VALUE);
  17.   }
  18.  
  19.   public static boolean bst(BinaryTree tree, int min, int max){
  20.   // TODO
  21.   if(tree == null){
  22.     return true;
  23.   }
  24.   if(tree.getKey() < min || tree.getKey() > max){
  25.     return false;
  26.   }
  27.  
  28.   return bst(tree.getLeft(), min, tree.getKey() - 1) && bst(tree.getRight(), tree.getKey()+1, max);
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement