Validate Binary Search Tree Problem Statement
Given a binary tree with 'N' nodes, determine if it is a Binary Search Tree (BST). Return true
if the tree is a BST, otherwise return false
.
Example:
Input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
true
Explanation:
The tree is a valid Binary Search Tree because it satisfies the following properties:
- The left subtree of a node contains only nodes with values less than the node’s value.
- The right subtree of a node contains only nodes with values greater than the node’s value.
- Both the left and right subtrees must also be binary search trees.
Input:
The first line contains an integer 't', the number of test cases. For each test case, the elements of the tree are provided in level order, with '-1' indicating a null node.
Output:
For each test case, output 'true' if the tree is a BST, otherwise output 'false'. Each result is printed on a new line.
Constraints:
1 <= T <= 100
1 <= N <= 1000
-10^6 <= data <= 10^6
- Duplicate elements can be in either the left or right subtree.
Note:
You do not need to print anything explicitly. Implement the function to return the required results.

AnswerBot
4mo
Validate if a given binary tree is a Binary Search Tree (BST) or not.
Check if the left subtree of a node contains only nodes with values less than the node's value.
Check if the right subtree of a node...read more
Help your peers!
Add answer anonymously...
Amazon SDE-2 interview questions & answers
A SDE-2 was asked Q. Given a binary tree where each node contains a digit from 0-9, find the sum of a...read more
A SDE-2 was asked Q. Write a program to perform a preorder traversal of a binary tree without using r...read more
A SDE-2 was asked Q. Write code to find the median of an unsorted array.
Popular interview questions of SDE-2
A SDE-2 was asked Q1. Given a binary tree where each node contains a digit from 0-9, find the sum of a...read more
A SDE-2 was asked Q2. Write a program to perform a preorder traversal of a binary tree without using r...read more
A SDE-2 was asked Q3. Write code to find the median of an unsorted array.
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

