
Asked in Blackrock
Validate Binary Search Tree Problem Statement
Your task is to determine if a given binary tree with 'N' nodes is a valid Binary Search Tree (BST). A BST is defined by the following properties:
- The left subtree of a node has only nodes with data less than the node's data.
- The right subtree of a node has only nodes with data greater than the node's data.
- Both the left and right subtrees must also be binary search trees.
Input:
The input consists of multiple test cases. Each test case provides the level order traversal of the binary tree as follows:
The first line contains an integer 'T', the number of test cases.
Each test case contains a line with space-separated integers representing the values of nodes in level order. A value of -1 indicates a null node in the tree.
Output:
For each test case, output 'true' if the binary tree is a BST, otherwise output 'false'. Each result is displayed on a new line.
Example:
Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
false
Constraints:
- 1 ≤ T ≤ 100
- 1 ≤ N ≤ 5000
- -106 <= data ≤ 106, and data ≠ -1
Note:
You are only required to implement the validation function for a BST. The input and output are managed elsewhere.

AnswerBot
4mo
Validate if a given binary tree is a valid Binary Search Tree (BST) based on its properties.
Check if the left subtree of a node has only nodes with data less than the node's data.
Verify if the right s...read more
Help your peers!
Add answer anonymously...
Top Software Developer Interview Questions Asked at Blackrock
Q. Bridge in Graph Problem Statement Given an undirected graph with V vertices and ...read more
Q. Shortest Distance in a Binary Search Tree Your task is to determine the shortest...read more
Q. Greatest Common Divisor Problem Statement You are tasked with finding the greate...read more
Interview Questions Asked to Software Developer at Other Companies
Top Skill-Based Questions for Blackrock Software Developer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
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

