566. Evaluate Postfix Expression

EasyStackStackMath

Given a list of tokens representing a valid postfix (Reverse Polish) expression with integer operands and the operators +, -, *, /, evaluate it and return the integer result. Division truncates toward zero. The input is JSON {tokens}.

Input: JSON {tokens} — a list of string tokens.

Output: Integer — the evaluated result.

Examples

Example 1
Input: {"tokens":["2","3","1","*","+","9","-"]}
Output: -4
Explanation: 2 + (3*1) - 9 = -4.
Example 2
Input: {"tokens":["5"]}
Output: 5
Explanation: A single operand.
Example 3
Input: {"tokens":["4","2","/"]}
Output: 2
Explanation: 4 / 2 = 2.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →