INPUT FILE: eval.in
OUTPUT FILE: eval.out
Often many expressions in regular mathematical notation such as 5 * (4 + 3) involve one or more sets of parentheses which make evaluation difficult. However, when we express a mathematical expression as a binary tree parentheses are not needed. For this example,
* / \ 5 + / \ 4 3
We cannot multiply the 5 without first evaluating 4 + 3.
Your task is to evaluate such an expression given the entries in the binary
tree.
INPUT
The input file contains 5 data sets.
The first line of the data gives the number of rows 1<=N<=10 in the binary
tree.
The next N lines each contain the elements of the tree, level-by-level from
left to right. Each element will be either an operator (*, +, - and / (integer
division, guaranteed to never cause you to divide by zero)) or a number from
1 to 9. It is guaranteed that the given tree constitutes a legal expression
(operators have 2 children, numbers have none). Furthermore, it is guaranteed
that evaluating any subtree of the given tree will result in an integer between
-32000 and 32000.
OUTPUT
Give the number to which each input evaluates.
Sample Input File
3 * 5 + 4 3 2 / 5 3
(and 3 more inputs)
Output for Sample Input
35 1