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!
Select
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
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits