You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
Evaluate the expression. Return an integer that represents the value of the expression.
Note that:
'+', '-', '*', and '/'.
Example 1:
Input: tokens = ["2","1","+","3","*"] Output: 9 Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"] Output: 6 Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"] Output: 22 Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = ((10 * (6 / (12 * -11))) + 17) + 5 = ((10 * (6 / -132)) + 17) + 5 = ((10 * 0) + 17) + 5 = (0 + 17) + 5 = 17 + 5 = 22
Constraints:
1 <= tokens.length <= 104tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].Problem Overview: You receive an array of tokens representing a mathematical expression in Reverse Polish Notation (postfix form). Evaluate the expression and return the final integer result while handling operators like +, -, *, and /.
Approach 1: Stack-Based Evaluation (O(n) time, O(n) space)
The natural way to evaluate Reverse Polish Notation is with a stack. Iterate through each token in the array. If the token is a number, push it onto the stack. If the token is an operator, pop the top two numbers from the stack, apply the operation in the correct order, and push the result back.
This works because postfix notation guarantees that every operator appears after its operands. The stack always holds intermediate results, and each operation reduces two values into one. Continue processing tokens until the end of the array; the stack's top element is the final result. Every token is processed exactly once, giving O(n) time complexity with O(n) space for the stack.
This problem directly tests your understanding of the stack data structure along with simple arithmetic from math. The tokens themselves are stored in an array, and the algorithm iterates through them sequentially.
Approach 2: Recursive Expression Evaluation (O(n) time, O(n) space)
Another way is to evaluate the expression recursively from the end of the token list. When you encounter an operator, recursively compute the right operand and then the left operand, then apply the operator. Each recursive call resolves a sub-expression and returns its value to the caller.
This approach mirrors how expression trees are evaluated, but it is less common in interviews because recursion makes the implementation harder to reason about and debug. It still processes each token once, resulting in O(n) time complexity, with O(n) space used by the recursion call stack.
Recommended for interviews: The stack-based approach is the expected solution. It directly models how postfix expressions are evaluated and demonstrates strong understanding of stacks and expression parsing. Showing awareness of recursive evaluation helps conceptually, but implementing the stack solution cleanly is what interviewers typically look for.
The stack-based approach is ideal for evaluating Reverse Polish Notation (RPN) expressions because it naturally follows the Last-In-First-Out (LIFO) principle, which aligns with the evaluation order of RPN expressions. The key idea is to iterate through the tokens, pushing operands onto the stack, and handling operators by popping the required number of operands from the stack, performing the operation, and pushing the result back onto the stack.
The program uses an integer array as a stack to evaluate the RPN expression. As it processes each token:
The final result is the only element left on the stack.
Time Complexity: O(n), where n is the number of tokens.
Space Complexity: O(n) due to stack usage.
| Approach | Complexity |
|---|---|
| Stack-Based Evaluation | Time Complexity: O(n), where n is the number of tokens. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Stack-Based Evaluation | O(n) | O(n) | Standard and most intuitive solution for postfix expressions |
| Recursive Evaluation | O(n) | O(n) | Useful when modeling the expression as recursive subproblems |
| Array as Manual Stack | O(n) | O(n) | Optimized implementations where dynamic stack objects are avoided |
Evaluate Reverse Polish Notation - Leetcode 150 - Python • NeetCode • 172,034 views views
Watch 9 more video solutions →Practice Evaluate Reverse Polish Notation with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor