Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Trees: Is This a Binary Search Tree? solution

YASH PAL, 31 July 2024

In this HackerRank Trees: Is This a Binary Search Tree? Interview preparation kit problem You are Given the root node of a binary tree, determine if it is a binary search tree.

HackerRank Trees: Is This a Binary Search Tree? solution

Foundation course by prepbytes

Problem solution in Python programming.

""" Node is defined as
class node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
"""

""" Node is defined as
class node:
  def __init__(self, data):
      self.data = data
      self.left = None
      self.right = None
"""

def checkBST(root):
    return check_BST_subtree(root, -1, 10001)    
    
def check_BST_subtree(root, min_value, max_value):
    if(root == None): return True
    
    data = root.data
    
    if(min_value < data < max_value):
        return check_BST_subtree(root.left, min_value, data) and check_BST_subtree(root.right, data, max_value)
    else:
        return False

Problem solution in Java Programming.

/* 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.  

The Node class is defined as follows:
    class Node {
        int data;
        Node left;
        Node right;
     }
*/
    boolean checkBST(Node root) {
        return checkBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    boolean checkBST(Node node, int min, int max){
        return node==null
            || (
            (min<node.data && node.data<max) 
            && checkBST(node.left, min, node.data) && checkBST(node.right,node.data, max)
        );
    }

Problem solution in C++ programming.

/* 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.  

The Node struct is defined as follows:
  struct Node {
    int data;
    Node* left;
    Node* right;
  }
*/
   bool checkBST(Node* root) {
        static Node *prev=NULL;
        if (root){  
        if (!checkBST(root->left))  
        return false;  
        if (prev != NULL && root->data <= prev->data)  
        return false;  
        prev = root;  
        return checkBST(root->right);  
      }  
   return true;    
}

coding problems interview prepration kit

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes