Advertisement
minnera

ctci-is-binary-search-tree

Sep 29th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. //https://www.hackerrank.com/challenges/ctci-is-binary-search-tree
  2.  
  3. /* Hidden stub code will pass a root argument to the function below. Complete the function to solve the challenge. Hint: you may want to write one or more helper functions.  
  4.  
  5. The Node class is defined as follows:
  6.     class Node {
  7.         int data;
  8.         Node left;
  9.         Node right;
  10.      }
  11. */
  12.     boolean checkBST(Node root) {
  13.         if(root.left != null){            
  14.             if(root.left.data < root.data){//ez a helyes
  15.                 if(!checkBST(root.left)){
  16.                     return false;
  17.                 }
  18.             }
  19.             else{//nem felel meg
  20.                 return false;
  21.             }
  22.         }
  23.         if(root.right != null){
  24.             if(root.right.data > root.data){//ez a helyes
  25.                 if(!checkBST(root.right)){
  26.                     return false;
  27.                 }
  28.             }
  29.             else{
  30.                 return false;
  31.             }
  32.         }
  33.         return true;
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement