

1. The first character of the string is guaranteed to be an integer which denotes the root element of the tree.
2. This is followed by zero, one or two pairs of parenthesis, which contains a child binary tree with the same structure.
3. The first parenthesis denotes the left child of the binary then the right child.
Let str= “1(2)(3)”
The tree will be:

The first element i.e 1 is the root node and the first bracket signifies the left child of the root node which has only 1 element i.e 2 and the second bracket signifies the right child of the root node that has only 1 node i.e 3 . Hence we have the binary tree as shown above.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test cases follow.
The first line of each test case contains the string ‘str’.
For each test case, return the root node of the constructed binary tree.
The printed output is the preorder traversal of the binary tree.
Output for each test case will be printed in a new line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 1000
Where 'N' denotes the length of the string 'str'
Time limit: 1 second
The key idea in solving this problem is to construct the tree recursively and then print the pre-order traversal.
Algorithm