Asked inAmazon,SDE-2

Validate Binary Search Tree Problem Statement

Given a binary tree with 'N' nodes, determine if it is a Binary Search Tree (BST). Return true if the tree is a BST, otherwise return false.

Example:

Input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
true
Explanation:

The tree is a valid Binary Search Tree because it satisfies the following properties:

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

Input:

The first line contains an integer 't', the number of test cases. For each test case, the elements of the tree are provided in level order, with '-1' indicating a null node.

Output:

For each test case, output 'true' if the tree is a BST, otherwise output 'false'. Each result is printed on a new line.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 1000
  • -10^6 <= data <= 10^6
  • Duplicate elements can be in either the left or right subtree.
Note:
You do not need to print anything explicitly. Implement the function to return the required results.
Be the first one to answer
Add answer anonymously...
Amazon SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter