Evaluate Reverse Polish Notation - Solution & Explanation
Problem Statement
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:
- The valid operators are
'+','-','*', and'/'. - Each operand may be an integer or another expression.
- The division between two integers always truncates toward zero.
- There will not be any division by zero.
- The input represents a valid arithmetic expression in a reverse polish notation.
- The answer and all the intermediate calculations can be represented in a 32-bit integer.
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].
Approach Overview
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.
Approach 1: Stack-Based Evaluation
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:
- If the token is a number, it's converted and pushed onto the stack.
- If it's an operator, the required numbers are popped from the stack, the operation is performed, and the result is pushed back onto the stack.
The final result is the only element left on the stack.
Complexity
Time Complexity: O(n), where n is the number of tokens.
Space Complexity: O(n) due to stack usage.
Approach 2: Default Approach
Try this approach in the editor →Complexity Comparison
| Approach | Complexity |
|---|---|
| Stack-Based Evaluation | Time Complexity: O(n), where n is the number of tokens. |
| Default Approach | — |
Detailed Complexity Analysis
| 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 |
Video Solution
Evaluate Reverse Polish Notation - Leetcode 150 - Python • NeetCode • 181,778 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Evaluate Reverse Polish Notation easy or hard?
Evaluate Reverse Polish Notation Python/Java solution
How to solve Evaluate Reverse Polish Notation in O(n)?
What is the best approach for Evaluate Reverse Polish Notation?
Is Evaluate Reverse Polish Notation asked at Google/Amazon/Meta?
What data structure is used in Evaluate Reverse Polish Notation?
What is the time complexity of Evaluate Reverse Polish Notation?
Ready to solve this problem?
Practice Evaluate Reverse Polish Notation with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor