Postfix to Infix & Evaluation of Postfix (Leetcode 150)

HardLeetcode
⭐⭐⭐

Postfix to Infix & Evaluation of Postfix (Leetcode 150)

Pattern:

Idea:

Variations :


πŸ’» Code

It has two parts - converting to infix and evaluating the expression . See both below. Both are O(n) in time and aux. space complexity


Postfix to Infix & Evaluation of Postfix (Leetcode 150)

Tags: #Stack #Expressions #Parsing #ReversePolishNotation #Postfix #Strings #Interview-Pattern #LeetCode #FAANG

Problem Statement

Postfix (Reverse Polish Notation) expressions place operators after their operands.

Two common interview problems are:

  1. Postfix β†’ Infix: Reconstruct the equivalent infix expression.

  2. Evaluate Postfix: Compute the numerical value of the expression (LC 150).

Examples

PostfixInfixValue
AB+C*(A+B)*Cβ€”
23+5*(2+3)*525
2 1 + 3 *(2+1)*39

Postfix Refresher

In postfix, operators always operate on the two most recent operands.

Example:

23+5*

Read left β†’ right

2 3 +  β†’ 5
5 5 *  β†’ 25

This naturally suggests a stack.


Part 1 β€” Postfix to Infix

Core Insight

Whenever an operator appears:

  • Pop the right operand

  • Pop the left operand

  • Form (left operator right)

  • Push the resulting expression back

The order of popping is extremely important.


Algorithm

For each character:

  • Operand β†’ Push onto stack

  • Operator β†’

    • right = pop()

    • left = pop()

    • Push ("(" + left + op + right + ")")

At the end, the stack contains one complete infix expression.


Python Implementation

def postfixToInfix(exp):

    stack = []

    for ch in exp:

        if ch.isalnum():
            stack.append(ch)

        else:
            right = stack.pop()
            left = stack.pop()

            stack.append("(" + left + ch + right + ")")

    return stack[-1]

Dry Run

Input

AB+C*
SymbolStack
AA
BA B
+(A+B)
C(A+B) C
*((A+B)*C)

Answer:

((A+B)*C)

Outer parentheses are harmless.


Why Right Is Popped First

Consider:

AB-

Correct infix:

A-B

Stack before -:

Top
B
A

Pop order:

right = B
left  = A

Result:

A-B

If reversed, we’d incorrectly obtain B-A.

Mnemonic: First pop = Right operand, Second pop = Left operand.


Part 2 β€” Evaluate Postfix (LC 150)

Problem Statement

Given a list of tokens representing a postfix expression, evaluate its value.

Division truncates toward zero.

Example:

["2","1","+","3","*"]

Output:

9

Core Insight

Exactly the same stack pattern, except we store integers instead of strings.

Algorithm

For each token:

  • Number β†’ Push

  • Operator β†’

    • Pop right

    • Pop left

    • Compute left op right

    • Push result

The final stack element is the answer.


Python Implementation

class Solution:
    def evalRPN(self, tokens):

        stack = []

        for token in tokens:

            if token not in "+-*/":
                stack.append(int(token))

            else:
                b = stack.pop()
                a = stack.pop()

                if token == "+":
                    stack.append(a + b)

                elif token == "-":
                    stack.append(a - b)

                elif token == "*":
                    stack.append(a * b)

                else:
                    stack.append(int(a / b))

        return stack[-1]

Dry Run

Tokens

["4","13","5","/","+"]

Stack Evolution

TokenStack
44
134 13
54 13 5
/4 2
+6

Answer = 6


Integer Division Caveat

Python’s // performs floor division, not truncation toward zero.

Example:

ExpressionRequired//
5/222
-5/2-2-3

Correct implementation:

int(a / b)

This truncates toward zero exactly as Leetcode specifies.


Correctness

Invariant

After processing any prefix of the postfix expression, the stack contains the values (or subexpressions) of all completely evaluated operands.

Whenever an operator appears:

  • The top two stack elements are exactly its operands.

  • Their order is preserved by popping right first.

Thus every operation is evaluated correctly.


Complexity

ProblemTimeAuxiliary Space
Postfix β†’ InfixO(n)O(n)
Evaluate PostfixO(n)O(n)

Each token is pushed and popped exactly once.


Common Mistakes

1. Reversing Operand Order

Wrong:

a = stack.pop()
b = stack.pop()
stack.append(a - b)

Correct:

right = stack.pop()
left = stack.pop()
stack.append(left - right)

This matters for - and /.

2. Using // for Division

Wrong:

stack.append(a // b)

Fails for negative values.

Correct:

stack.append(int(a / b))

3. Treating Digits as Characters

Leetcode provides tokens, not a continuous string.

Correct:

int(token)

not

ord(token)

Relationship to Expression Problems

Conversion / EvaluationStack Stores
Infix β†’ PostfixOperators
Infix β†’ PrefixOperators
Postfix β†’ InfixStrings
Prefix β†’ InfixStrings
Evaluate PostfixIntegers
Evaluate PrefixIntegers

The underlying pattern is identicalβ€”the only thing changing is what the stack represents.


Pattern Recognition

Expression Conversion

  • Stack stores partial expressions

  • Pop right, pop left, combine, push

Expression Evaluation

  • Stack stores computed values

  • Pop right, pop left, evaluate, push

Interview Heuristic: In postfix, every operator immediately consumes the two most recent operands, making the stack the natural evaluation model.

Local Graph View

Start typing to search
Try: two sum or #Arrays or #Amazon