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.
Be the first one to answer
Add answer anonymously...
Top Adobe Product Intern interview questions & answers
Popular interview questions of Product Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app