Validate BST Problem Statement

Given a binary tree with N nodes, determine whether the tree is a Binary Search Tree (BST). If it is a BST, return true; otherwise, return false.

A binary search tree (BST) is a binary tree with the following properties:

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

Input:

The first line contains an integer t, indicating the number of test cases. Each test case consists of a single line containing the elements of the tree in level order form separated by a single space. If any node does not have a left or right child, use -1 in its place.

Output:

For each test case, return true if the binary tree is a BST, otherwise return false. Output for each test case should be in a separate line.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
true
Explanation:
The given input corresponds to a tree where each node satisfies the properties of a BST.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 1000
  • -106 <= data <= 106

Where N is the number of nodes in the tree, T represents the number of test cases, and data denotes the data contained in each node of the binary tree.

Note:
You do not need to print anything explicitly. The implementation handles printing the result for each test case.
AnswerBot
4mo

Validate if a binary tree is a Binary Search Tree (BST) based on given properties.

  • Check if left subtree contains only nodes with data less than current node's data

  • Check if right subtree contains only ...read more

Help your peers!
Select
Add answer anonymously...

Adobe Product Intern interview questions & answers

A Product Intern was asked 5mo agoQ. Given a knapsack with a maximum weight capacity W and a set of items, each with ...read more
A Product Intern was asked Q. Explain C++ memory management and pointers.
A Product Intern was asked Q. What is the difference between a process and a thread?

Popular interview questions of Product Intern

A Product Intern was asked 5mo agoQ1. Given a knapsack with a maximum weight capacity W and a set of items, each with ...read more
A Product Intern was asked Q2. Explain C++ memory management and pointers.
A Product Intern was asked Q3. What is the difference between a process and a thread?
Adobe Product Intern 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