Size of Largest BST in a Binary Tree
Given a binary tree with 'N' nodes, determine the size of the largest subtree that is also a Binary Search Tree (BST).
Explanation:
A Binary Search Tree (BST) is defined as follows:
- 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', representing the number of test cases. Each test case follows: A single line with space-separated values representing the binary tree in level order. Use -1 for null nodes.
Output:
For each test case, return an integer that indicates the size of the largest BST subtree within the binary tree.
Example:
Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
3
Explanation: In the given tree, the largest subtree that is also a BST has 3 nodes.
Constraints:
- 1 <= T <= 100
- 1 <= N <= 5000
- 1 <= data <= 105 and data != -1
Note: You do not need to print anything; simply implement the function to return the result.

AnswerBot
4mo
Find the size of the largest BST subtree in a binary tree.
Traverse the tree in a bottom-up manner to check if each subtree is a BST.
Keep track of the size of the largest BST found so far.
Recursively c...read more
Help your peers!
Add answer anonymously...
Josh Technology Group Software Developer interview questions & answers
A Software Developer was asked 9mo agoQ. Given the head of a linked list, determine if the linked list has a cycle in it....read more
A Software Developer was asked 9mo agoQ. Given a binary tree, imagine yourself standing on the left side of it, return th...read more
A Software Developer was asked 11mo agoQ. What is the software development life cycle, and what steps does it involve?
Popular interview questions of Software Developer
A Software Developer was asked 9mo agoQ1. Given the head of a linked list, determine if the linked list has a cycle in it....read more
A Software Developer was asked 9mo agoQ2. Given a binary tree, imagine yourself standing on the left side of it, return th...read more
A Software Developer was asked 11mo agoQ3. What is the software development life cycle, and what steps does it involve?
>
Josh Technology Group Software Developer Interview 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

