Validate Partial Binary Search Tree (BST) Problem Statement

You are provided with a binary tree containing 'N' nodes. Your task is to determine if this tree is a Partial Binary Search Tree (BST). Return true if it is a Partial BST, otherwise return false.

Definition of a Partial BST:

• Each node's left subtree contains nodes with data less than or equal to the node's data.
• Each node's right subtree contains nodes with data greater than or equal to the node's data.
• Both left and right subtrees must be partial binary search trees themselves.

Example:

Input:

BST1

Output:
true
Explanation:
Level 1: All nodes in the left subtree of 4 (2, 1, 3) are smaller than 4, and those in the right subtree (5) are larger than 4.
Level 2:
- For node 2: All nodes in its left subtree (1) are smaller than 2, and those in the right subtree (3) are larger than 2.
- For node 5: Both left and right subtrees are empty.
Level 3:
- For nodes 1 and 3, both left and right subtrees are empty.
Since all nodes follow the Partial BST properties, the tree is a Partial BST.

Input Format:

The first line contains an integer 't', the number of test cases. 
Each test case consists of a single line representing the tree's level order traversal, separated by spaces. Use '-1' to represent a null node.
Example: '1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1'

Output Format:

Return 'true' or 'false' for each test case as a separate line.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 1000
  • -10^6 <= data <= 10^6
  • Duplicate elements can be in either or both subtrees.
  • Time Limit: 1 sec

Note:

Do not print anything under normal operations. Implement the function as described in the problem statement.
AnswerBot
13d

Validate if a binary tree is a Partial Binary Search Tree (BST) by checking if each node's left subtree contains nodes with data less than or equal to the node's data, and each node's right subtree co...read more

Help your peers!
Add answer anonymously...
Cadence Design Systems Software Developer 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