Medium · Stacks & Queues

Calculate postfix

Given an array tokens that forms a valid postfix (Reverse Polish) expression, where each token is an integer (possibly negative) or one of the operators +, -, * and /, return the value of the expression, with division truncating toward zero.

Examples

Example 1

{
  "tokens": ["2", "1", "+", "3", "*"]
}

Output: = 9

Example 2

{
  "tokens": ["4", "13", "5", "/", "+"]
}

Output: = 6

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Stacks & Queues problems