Ninja has got a coding assignment. In this assignment, a preorder depth-first search (DFS) is run on the root of a binary tree.
At each node in this traversal, D dashes is given (where D is the depth of this node), then we output the value of this node. If the depth of a node is D, the depth of its immediate child is D + 1. The depth of the root node is 0.
It is also given to Ninja that if a node has only one child, that child is guaranteed to be the left child.
The output S of this traversal is given to Ninja. Your task is to help Ninja in recovering the tree and return its root.
Example
Let S = "1-2--3--4-5--6--7"
Here the tree for this preorder traversal will be like following:

So for this root will be 1, so the output tree will be 1 2 5 3 4 6 7 -1 - 1 -1 -1 -1 -1- 1 -1
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first and the only line of each test case contains a string S representing the preorder traversal of the input tree.
Output format :
For each test case, you have to return the root of the tree.
Note: You do not need to take input or print anything. It already has been taken care of. Just implement the function.
1 <= T <= 100
1 <= X <= 5000 where,X is the number of nodes in original tree
1 <= Y <= 10^9 where, Y is the value of node
Time limit: 1 sec
2
1-2--3--4-5--6--7
1-5--3-6--4
1 2 5 3 4 6 7 -1 - 1 -1 -1 -1 -1- 1 -1
1 5 6 3 -1 4 -1 -1 -1 -1 -1
Test case 1 :
The tree for this preorder traversal will be like following:

So the output tree would be 1 2 5 3 4 6 7 -1 - 1 -1 -1 -1 -1- 1 -1
Test case 2 :
The tree for this preorder traversal would be:

So the output tree will be 1 5 6 3 -1 4 -1 -1 -1 -1 -1
2
1-2--3---4-5--6---7
1-401--349---90--88
1 2 5 3 -1 6 -1 4 -1 7 -1 -1 -1 -1 -1
1 401 -1 349 88 90 -1 -1 -1 -1 -1
Think of preorder traversal of a tree and how you can use ‘-’ to recover the tree.
Algorithm:
O(S) where S is the length of the string.
We are iterating over the whole string so the complexity will be O(S)
O(n) where n is the number of nodes in the tree.
We are storing the nodes in a stack. So the complexity will be O(n)