SHARE
TWEET

Untitled

a guest Jan 14th, 2020 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top