
‘N’ = 4, ‘ARR1’ = [2, 4, 1, 3] and ‘ARR2’ = [2, 4, 3, 1]
The following diagram shows how to construct a BST from the input array (you have to solve this problem without actually constructing the tree)
BST for ‘ARR1’ = [2, 4, 1, 3]:

BST for ‘ARR2’ = [2, 4, 3, 1]:

As the BSTs constructed from ‘ARR1’ and ‘ARR2’ are identical, you should return ‘True’ as the answer.
The first line of input contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first line of input contains a single integer ‘N’ denoting the size of the arrays ‘ARR1’ and ‘ARR2’.
The second line of each test case contains ‘N’ space-separated integers representing the array ‘ARR1’.
The third line of each test case contains ‘N’ space-separated integers representing the array ‘ARR2’.
For each test case, print ‘True’ if the BSTs constructed from ‘ARR1’ and ‘ARR2’ are identical. Otherwise, return ‘False’.
The output of each test case will be printed in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 3*10^3
The values in the arrays ‘ARR1’ and ‘ARR2’ range from ‘[1, 10^9]’.
Time Limit: 1 sec
We know that the ‘0-th’ index of the given array is the root of the BST constructed from it.
Apart from these two conditions, we need to check that the left child is greater than the parent of the root node (and smaller than the root node). The right child should be smaller than the parent of the root node (and greater than the root node).
To achieve this, we can recursively call for the left and right subtrees while maintaining two variables, ‘MIN’ and ‘MAX’, in the recursive function. The values ‘MIN + 1’ and ‘MAX - 1’ denote the minimum and maximum allowed values for the current root node. These values depend on whether the current root node is the left or right child of its parent node.
For the left child, ‘MIN = The value of the parent of root node’ and ‘MAX = the value of root node’.
For the right child, ‘MIN = The value of root node’ and ‘MAX = The value of the parent of root node’.
In the recursive function, check that the root node should be the same in both the arrays (or BSTs). Otherwise, return ‘False’. We break the recursion if the root node’s index in both the arrays is equal to ‘N’, i.e., when the root node’s parent is a leaf node in both the BSTs.
ALGORITHM:
For the following function, ‘i1’ and ‘i2’ are the indexes from which we need to search for the root node for ‘ARR1’ and ‘ARR2’, respectively.
‘Boolean isIdenticalBSTUtil(array ARR1, array ARR2, integer N, integer i1, integer i2, integer MIN, integer MAX)’:
Guess Price
Unique BSTs
Unique BSTs
Unique BSTs
Kth Largest Element in BST
Two Sum IV - Input is a BST
Icarus and BSTCOUNT