Given a binary tree, Your task is to complete the function largestBst that returns the size of the largest subtree which is also a Binary Search Tree (BST). If the whole Binary Tree is BST, then return the size of the whole tree.
Input:
The first line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
For example:
For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
Output:
The function should return the size of the max subtree which is also the BST.
Your Task:
This is a function problem. You don't have to take any input. Just complete the function largestBst() that takes a node as a parameter and return the size of the max subtree which is also the BST.
Constraints:
1 <=T <= 10
1 <= Number of nodes <= 100000
1 <= Data of a node <= 1000000
Example:
Input
2
1 4 4 6 8
6 6 3 N 2 9 3 N 8 8 2
Output
1
2
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.
saiujwal13083 | 347 |
isiddhisingh | 252 |
Abhishek_Jadhav | 243 |
_uncle_sam_ | 221 |
c_ocoooo | 217 |
saiujwal13083 | 610 |
SumitSingh27 | 466 |
c_ocoooo | 456 |
rathiarpit29 | 379 |
NaveenKumarNakka | 370 |
blackshadows | 5362 |
Ibrahim Nash | 5242 |
akhayrutdinov | 5111 |
mb1973 | 4931 |
Quandray | 4598 |