Check for BST
Easy Accuracy: 21.58% Submissions: 100k+ Points: 2

Given the root of a binary tree. Check whether it is a BST or not.
Note: We are considering that BSTs can not contain duplicate Nodes.

BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.


Example 1:

 /    \
1      3
Output: 1 
The left subtree of root node contains node 
with key lesser than the root node’s key and 
the right subtree of root node contains node 
with key greater than the root node’s key.
Hence, the tree is a BST.

Example 2:

Output: 0 
Since the node with value 7 has right subtree 
nodes with keys less than 7, this is not a BST.

Your Task:
You don't need to read input or print anything. Your task is to complete the function 
isBST() which takes the root of the tree as a parameter and returns true if the given binary tree is BST, else returns false

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the BST).

0 <= Number of edges <= 100000

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Check for BST

Output Window