
Asked in Oracle
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 the left subtree of a node contains only nodes with data less than the node's data.
Verify if the right subtre...read more
Help your peers!
Add answer anonymously...
Top Full Stack Developer Interview Questions Asked at Oracle
Q. Implement a few design patterns.
Q. Bursting Balloons Problem Given an array ARR of size N, where each element repre...read more
Q. Root to Leaf Path Problem Statement Given a binary tree with 'N' nodes numbered ...read more
Interview Questions Asked to Full Stack Developer at Other Companies
Top Skill-Based Questions for Oracle Full Stack Developer
Web Development Interview Questions and Answers
250 Questions
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
JavaScript Interview Questions and Answers
250 Questions
Java 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

