Evaluate Postfix Expression
Mediumjavapythonccppjavascript
Given a valid postfix (Reverse Polish) expression of integers and the operators +, -, *, /, evaluate it. Division truncates toward zero.
Input Format
The first line contains an integer N, the number of tokens. The second line contains N space-separated tokens: each is an integer or one of + - * /.
Output Format
Print the integer value of the expression.
Example 1
Input
5 2 3 + 4 *
Output
20
Explanation: 2 + 3 = 5, then 5 * 4 = 20.
- 1 <= N <= 100000
- Every intermediate and final value fits in a 64-bit signed integer.
- No division by zero occurs.