You are given a ternary expression in the form of a string. Your task is to convert this expression into a binary tree.
Note:1. The string is made up of lowercase English alphabets, ‘?’ and ‘:’ special characters.
2. The alphabets may be repeated in the string.
3. The expression which uses ‘?:’ is known as ternary expression and the expression will be evaluated as (condition ? value_if_true : value_if_false).
The first line of input contains a single integer 'T', representing the number of test cases.
The first and the only line of each test case contains a string 'STR' representing the ternary expression.
Output format:
For each test case, return the level order traversal of the resultant binary tree in a space separated elements in a single line . If a node is NULL then # will be considered at its place.
Note :
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1<= N <= 10^4
Where ‘N’ is the length of the string.
Time limit: 1 sec
2
a?b:c
a?b?c:d
a b c # # # #
a b # c d # # # #
In test case 1, The tree for the string will be:

In test case 2, The tree for the string will be:

2
j?k:l?m:o
m?n?p:o?o
j k l # # m o # # # #
m n # p o # # o # # #
In test case 1, The tree for the string will be:

In test case 2,The tree for the string will be:

Think about recursion.
The idea is to use recursion and the tree will be built in a bottom-up approach. As for each node, we need to find its left and right child, but the child will have to find their children first and only then they can be assigned to their parent.
The steps are as follows:
O(N), Where ‘N' is the length of the string.
Since we are traversing the string only once and thus the time complexity will be O(N).
O(N), Where N is the length of the string.
Since the total number of calls made by recursion will be equal to the number of times ‘?’ will appears. And ‘?’ can appear max of N/2 times, in the case of a skew tree. Thus the space complexity will be O(N).