Is This a Binary Search Tree?

Problem Statement :

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements:

The data value of every node in a node's left subtree is less than the data value of that node.
The data value of every node in a node's right subtree is greater than the data value of that node.
Given the root node of a binary tree, can you determine if it's also a binary search tree?

Complete the function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must return a boolean denoting whether or not the binary tree is a binary search tree. You may have to write one or more helper functions to complete this challenge.

Input Format

You are not responsible for reading any input from stdin. Hidden code stubs will assemble a binary tree and pass its root node to your function as an argument.


 0 <= data <= 10^4

Output Format

You are not responsible for printing any output to stdout. Your function must return true if the tree is a binary search tree; otherwise, it must return false. Hidden code stubs will print this result as a Yes or No answer on a new line.

Solution :


                            Solution in C :

In C ++ :

The Node struct is defined as follows:
	struct Node {
		int data;
		Node* left;
		Node* right;
*/  enum comp {LESS, GREATER};
    bool checkBST(Node* root,int minVal, int maxVal ){
            return true;
        int nVal=root->data;
        if(nVal<=minVal || nVal>=maxVal)
            return false;
        return checkBST(root->left,minVal,nVal) && checkBST(root->right,nVal, maxVal);
	bool checkBST(Node* root) {
            return true;
        return checkBST(root->left, -1, root->data) && checkBST(root->right,root->data, 10001);

In Java :

The Node class is defined as follows:
    class Node {
    int data;
    Node left;
    Node right;
    private boolean checkBST(Node n, int min, int max) {
        if(n == null) return true;
        return > min && < max && checkBST(n.left, min, && checkBST(n.right,, max);

    boolean checkBST(Node root) {
        if(root == null) return true;
        if(count == 0) return true;
        return checkBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);

In  python3 :

from collections import deque
""" Node is defined as
class node:
  def __init__(self, data): = data
      self.left = None
      self.right = None
def check_binary_search_tree_(root, lowest_value=0, highest_value=10000):
    min_v = lowest_value - 1
    max_v = highest_value + 1
    q = deque([(root, min_v, max_v)])
    while q:
        node, min_val, max_val = q.popleft()
        if not node: continue
        if >= max_val or <= min_val: return False
        if node.left: q.append((node.left, min_val,
        if node.right: q.append((node.right,, max_val))
    return True

View More Similar Problems

Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →

The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →

Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →

Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →