733. Design An Expression Tree With Evaluate Function
Implement an Expression Tree for evaluating arithmetic expressions. Each node of the tree represents either:
- An operand (integer).
- An operator (+, -, *, /).
You need to:
- Build an expression tree from a given postfix expression.
- Evaluate the expression tree.
Example 1:
Input: s = ["3", "4", "+", "2", "*", "7", "/"]
Output: 2
Explanation:
This expression evaluates to the above binary tree with expression ((3+4)*2)/7 = 14/7 = 2.
Example 2:
Input: s = ["4", "5", "2", "7", "+", "-", "*"]
Output: -16
Explanation:
This expression evaluates to the above binary tree with expression 4 * (5 - (2 + 7)) = 4 * (-4) = -16.
Now Your Turn!
Pick the correct output for the given inputInput: s=["5", "3", "4", "+", "5", "*"]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= s.length < 100
- s.length is odd.
- s consists of numbers and the characters '+', '-', '*', and '/'.
- If s[i] is a number, its integer representation is no more than 105.
- It is guaranteed that s is a valid expression.
- The absolute value of the result and intermediate values will not exceed 109.
- It is guaranteed that no expression will include division by zero.