# HackerRank Trees: Is This a Binary Search Tree? solution

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.

## 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;
}```