Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Trees: Is This a Binary Search Tree? solution

YASH PAL, 31 July 20246 February 2026

In this HackerRank Trees: Is This a Binary Search Tree? Problem Solution: You are given the root node of a binary tree. Determine if it is a binary search tree.

Function Description

Complete the function checkBST in the editor below. It must return a boolean denoting whether or not the binary tree is a binary search tree.

checkBST has the following parameter(s):

  • root: a reference to the root node of a tree to test
HackerRank Trees: Is This a Binary Search Tree? solution

HackerRank Trees: Is This a Binary Search Tree? solution in Python.

""" 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

Trees: Is This a Binary Search Tree? Solution in Java.

/* 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 solutions Hackerrank Problems Solutions interview prepration kit HackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes