You are given a binary search tree consisting of distinct elements. The binary search tree is created by traversing through the sequence from left to right and inserting each element. You need to print all the sequences or ways that would result in creating the given BST.
Note:Keep in mind that while merging the sequences the relative order of elements should be preserved.
For example :
For the given binary search tree

The valid BST sequences for the above BST are:
4 2 1 3 5 6
4 2 1 5 3 6
4 2 1 5 6 3
4 2 3 1 5 6
4 2 3 5 1 6
4 2 3 5 6 1
4 2 5 1 3 6
4 2 5 1 6 3
4 2 5 3 1 6
4 2 5 3 6 1
4 2 5 6 1 3
4 2 5 6 3 1
4 5 2 1 3 6
4 5 2 1 6 3
4 5 2 3 1 6
4 5 2 3 6 1
4 5 2 6 1 3
4 5 2 6 3 1
4 5 6 2 1 3
4 5 6 2 3 1
You need to print all of them.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image would be :

Input Format:
5
4 7
2 -1 6 8
-1 3 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 5
Level 2 :
Left child of 5 = 4
Right child of 5 = 7
Level 3 :
Left child of 4 = 2
Right child of 4 = null (-1)
Left child of 7 = 6
Right child of 7 = 8
Level 4 :
Left child of 2 = null (-1)
Right child of 2 = 3
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Left child of 8 = null (-1)
Right child of 8 = null (-1)
Level 5 :
Left child of 3 = null (-1)
Right child of 3 = null (-1)
The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null(-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
5 4 7 2 -1 6 8 -1 3 -1 -1 -1 -1 -1 -1
Output Format:
For each test case, print all the valid BST sequences of the given Binary Search Tree in a separate line.
Print the output of each test case in sorted order.
Print the output of each test case in a separate line.
Note :
You do not need to print anything; it has already been taken care of. You just need to store all valid sequences of the given BST in a predefined data structure.
1 <= T <= 10
0 <= N <= 10
1 <= data <= 10^4
Time Limit: 1sec
1
4 2 5 1 3 -1 6 -1 -1 -1 -1 -1 -1
4 2 1 3 5 6
4 2 1 5 3 6
4 2 1 5 6 3
4 2 3 1 5 6
4 2 3 5 1 6
4 2 3 5 6 1
4 2 5 1 3 6
4 2 5 1 6 3
4 2 5 3 1 6
4 2 5 3 6 1
4 2 5 6 1 3
4 2 5 6 3 1
4 5 2 1 3 6
4 5 2 1 6 3
4 5 2 3 1 6
4 5 2 3 6 1
4 5 2 6 1 3
4 5 2 6 3 1
4 5 6 2 1 3
4 5 6 2 3 1
The binary search tree will look like this:

In the above Binary Search Tree, all the valid sequences are:
4 2 1 3 5 6
4 2 1 5 3 6
4 2 1 5 6 3
4 2 5 1 3 6
4 2 5 1 6 3
4 2 5 6 1 3
4 5 2 1 3 6
4 5 2 1 6 3
4 5 2 6 1 3
4 5 6 2 1 3
4 2 3 1 5 6
4 2 3 5 1 6
4 2 3 5 6 1
4 2 5 3 1 6
4 2 5 3 6 1
4 2 5 6 3 1
4 5 2 3 1 6
4 5 2 3 6 1
4 5 2 6 3 1
4 5 6 2 3 1
2
2 1 3 -1 -1 -1 -1
7 -1 -1
2 1 3
2 3 1
7
Can you think about using recursion?
The basic idea of solving this question is to use the divide and conquer approach. The main idea behind this approach of generating sequences is that the “root” node is always going to be the first element of all possible sequences.
So, we will always start with the “root” node of the tree, as it is the only valid choice. Now for each of the rest valid choices, we will:
The recursion will end when we don't have any remaining nodes or choices left.
For recursion we will start with creating two base cases:
After creating the base cases we will divide the tree into left and right subtrees and recursively call for both these trees, and finally merging the solutions from the two subtrees.
Algorithm:
O(2 ^ N), Where ‘N’ is the number of nodes in the given binary search tree.
Since we are recursively calling for left and right subtrees for each node, the overall time complexity is
O(2 ^ N).
O(N), Where ‘N’ is the number of nodes in the given binary search tree.
Since we are storing the sequences of each node in a set and the final output in a vector, the overall space complexity is
O(N).