VOOZH about

URL: https://www.geeksforgeeks.org/dsa/prefix-postfix-conversion/

⇱ Prefix to Postfix Conversion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Prefix to Postfix Conversion

Last Updated : 2 May, 2026

Given a Prefix expression, convert it into a Postfix expression. A Prefix expression places the operator before the operands (operator operand1 operand2), for example *+AB-CD is represented as (A + B) * (C − D). A Postfix expression places the operator after the operands (operand1 operand2 operator), for example AB+CD-* is represented as (A + B) * (C − D).

Example: 

Input : Prefix : *+AB-CD
Output : AB+CD-*
Explanation : Prefix to Infix : (A+B) * (C-D)
Infix to Postfix : AB+CD-*

Input : Prefix : *-A/BC-/AKL
Output : ABC/-AK/L-*
Explanation : Prefix to Infix : (A-(B/C))*((A/K)-L)
Infix to Postfix : ABC/-AK/L-*

[Approach] - Using Stack - O(n) Time and O(n) Space

The idea is process from right to left so that we first see operands. We store operands in a stack. When an operator is found, the two most recent operands on the stack belong to it. Placing the operator after these operands forms the correct postfix expression

  • Traverse the prefix expression from right to left
  • If the symbol is an operand, push it onto the stack
  • If the symbol is an operator, pop two operands from the stack, form a postfix string: operand1 + operand2 + operator and push the result back onto the stack
  • Continue until the entire expression is processed
  • The remaining element in the stack is the postfix expression

Output
ABC/-AK/L-*
Comment