

You are given a string βSβ, an infix expression that has operands, operators and parentheses β(β and β)β.
You need to return the binary expression tree, whose inorder traversal is the same as βSβ.
Note:
Infix expression: The expression of the form βa operator bβ. When an operator is in-between every pair of operands.
The expression tree is a binary tree in which each internal node corresponds to the operator and each leaf node corresponds to the operand so for example expression tree for 5 * ( 6 - 3 ) / 2 - 8 would be:

Input Format:
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains a string S representing infix expression.
Output Format :
For each test case, return the binary expression tree, whose inorder traversal is the same as 'S'.
Return -1 instead of NULL.
The output for each test case is printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 5000
Operands are only numbers between 0 and 9 (included).
It is guaranteed that βSβ is a valid infix expression.
Time limit: 1 sec
2
5*(9-4)/7+6
5*7-8/6
[+, /, 6, *, 7, -1, -1, 5, -, -1, -1, -1, -1, 9, 4, -1, -1, -1, -1]
[-, *, /, 5, 7, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1]
Test Case 1: The expression tree for the given infix expression will be:

Test Case 2: The expression tree for the given infix expression will be:

2
7*6/4-2+4
7+(5/9)-6
[+, -, 4, /, 2, -1, -1, *, 4, -1, -1, 7, 6, -1, -1, -1, -1, -1, -1]
[-, +, 6, 7, /, -1, -1, -1, -1, 5, 9, -1, -1, -1, -1]
Try to use stacks to store the nodes.
The idea is that we will use two stacks, one for operators and the other for nodes but before all this, we need to first set the priority of each operator. We can use a map to set priority.
After setting the priority we will iterate through all characters of the given string βSTRβ and we will have 4 cases.
After this if stack size is still greater than 1 then we will make new nodes using operator and node stack till get size = 1 in the node stack.
Algorithm:
map<char,int> priority;
Set priority of β(β = 1, β-β = 2, β+β = 2, β*β =3 , β/β = 3
stack<char> operators;
stack<binarytreenode> tree
foreach(char c of s){
Check for all above cases one by one.
}
while(node.size > 1)
{
makeNode(operator, tree).
}
return tree.top().
O(N), where βNβ is the length of the given string.
We are checking each character of the string one by one.
O(N), where βNβ is the length of the given string.
In the worst case, due to parenthesis stack can contain all the operators.