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) or not.

  • Check if the left subtree of a node contains only nodes with data less than the node's data.

  • Check if the right subtree of a node contains...read more

Help your peers!
Select
Add answer anonymously...

Morgan Stanley Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. How would you design a system like Facebook?
A Software Developer Intern was asked Q. Ways To Make Coin Change Given an infinite supply of coins of varying denominati...read more
A Software Developer Intern was asked Q. Sorted Linked List to Balanced BST Problem Statement Given a singly linked list ...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. How would you design a system like Facebook?
A Software Developer Intern was asked Q2. Ways To Make Coin Change Given an infinite supply of coins of varying denominati...read more
A Software Developer Intern was asked Q3. Sorted Linked List to Balanced BST Problem Statement Given a singly linked list ...read more
Morgan Stanley Software Developer 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