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 are given an array of tokens representing an arithmetic expression written in Reverse Polish Notation (postfix). Evaluate the expression and return the final integer result. Operators appear after their operands, so you must process tokens in order and apply operations at the correct time.
Approach 1: Repeated List Reduction (O(n^2) time, O(1) extra space)
A straightforward way is to repeatedly scan the token array to find a pattern of number number operator. Once found, compute the result and replace the three elements with the computed value. Continue scanning and reducing the array until only one value remains. This works because postfix expressions guarantee that every operator eventually follows its operands. However, every reduction requires shifting elements or rebuilding the array, leading to O(n^2) time in the worst case. This approach is rarely used in production but helps you understand how postfix evaluation works.
Approach 2: Stack-Based Evaluation (O(n) time, O(n) space)
The standard solution uses a stack. Iterate through the tokens once. When you encounter a number, push it onto the stack. When you encounter an operator (+, -, *, /), pop the top two numbers from the stack, apply the operation, and push the result back. The order matters: the first popped value is the second operand. This works because postfix notation guarantees that operands appear before the operator that uses them.
Each token is processed exactly once, making the algorithm O(n) time. The stack may hold up to O(n) numbers in the worst case. This approach maps naturally to the structure of postfix expressions and is the standard interview solution. The implementation is straightforward in languages like Python, Java, and C++ using built-in stack or list structures.
The algorithm relies on simple operations: iterate through the array, push integers, and compute results using basic math operators. Division must truncate toward zero, which is a common edge case interviewers expect you to handle correctly.
Recommended for interviews: Interviewers expect the stack-based approach. It directly models how postfix expressions are evaluated and achieves optimal O(n) time. Mentioning the naive reduction idea shows conceptual understanding, but implementing the stack solution demonstrates practical problem-solving skill.
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.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of tokens.
Space Complexity: O(n) due to stack usage.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Repeated List Reduction | O(n^2) | O(1) | Conceptual understanding of postfix evaluation or very small inputs |
| Stack-Based Evaluation | O(n) | O(n) | General case and interview solution for evaluating Reverse Polish Notation |
Evaluate Reverse Polish Notation - Leetcode 150 - Python • NeetCode • 135,682 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