Tensorum
LeetCode 150 · Expression Evaluation

Evaluate Reverse Polish Notation

LeetCode 150 · Medium · solution, intuition & step-by-step walkthrough

Operands on a stack; operator pops two.

MediumArrayMathStack
3 msruntime · beats 29%
20.5 MBmemory · beats 75%
1approach
10accepted runs · last 15 Mar 2026

This is how I solved Evaluate Reverse Polish Notation (LeetCode 150), a medium problem from the Expression Evaluation pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

In RPN every operator applies to the two most recent values, so push numbers on a stack and, at an operator, pop two, combine, and push the result.

Approach

  1. 1

    Number → push.

  2. 2

    Operator → b = pop, a = pop, push a op b.

  3. 3

    Division truncates toward zero: int(a / b).

How I solved it

Stack evaluationPythonO(n) time
class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        #store the operation with operator as key in a map
        operations = {
            "*": lambda a,b:a*b,
            "+": lambda a,b:a+b,
            "-": lambda a,b:a-b,
            "/": lambda a,b:int(a/b)
        }
        stack = []
        for token in tokens:
            if token in operations:
                b = stack.pop()
                a = stack.pop()
                operation = operations[token]
                cur_val = operation(a,b)
                stack.append(cur_val)
            else:
                stack.append(int(token))
        return stack.pop()
Loading the step-through…

Evaluate Reverse Polish Notation — FAQ

What is the intuition behind Evaluate Reverse Polish Notation?

In RPN every operator applies to the two most recent values, so push numbers on a stack and, at an operator, pop two, combine, and push the result.

How do you solve Evaluate Reverse Polish Notation (LeetCode 150)?

1. Number → push. 2. Operator → b = pop, a = pop, push a op b. 3. Division truncates toward zero: int(a / b).

What is the time and space complexity of Evaluate Reverse Polish Notation?

Time: O(n). Space: O(n). (Accepted approach: Stack evaluation.)

What are common mistakes in Evaluate Reverse Polish Notation?

Pop order matters for − and /. Python's // floors negatives; int(a / b) truncates as required.