Postfix to Infix Conversion

Last Updated : 29 Aug, 2026

Postfix to infix conversion involves transforming expressions where operators follow their operands (postfix notation) into standard mathematical expressions with operators placed between operands (infix notation). This conversion improves readability and understanding.

  • Infix expression: The expression of the form a op b. When an operator is in-between every pair of operands. 
  • Postfix expression: The expression of the form a b op. When an operator is followed for every pair of operands. 

Given a string s that represents the postfix form of a valid mathematical expression. Convert it to its infix form.

Examples: 

Input: s = "ab*c+"
Output: ((a*b)+c)
Explanation: The postfix expression ab*c+ represents (a*b)+c. Therefore, its equivalent infix expression is ((a*b)+c).

Input: s = "abc/-"
Output: (a-(b/c))
Explanation: The postfix expression abc/- represents a-(b/c). Therefore, its equivalent infix expression is (a-(b/c)).

Try It Yourself
redirect icon

Using Recursion - O(n) Time and O(n) Space

The idea is to recursively process the postfix expression and construct the infix expression whenever an operator is encountered.

Working of Approach:

  • Start traversing the postfix expression from right to left.
  • If the character is an operand, return it.
  • If it is an operator, recursively get the right and left operands.
  • Combine them as (left operator right).
  • Return the final infix expression.
C++
#include <iostream>
using namespace std;

// Check whether the character is an operand.
bool isOperand(char x)
{
    return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z');
}

// Recursively convert postfix expression to infix.
string solve(string &s, int &i)
{

    // Take the current character from right to left.
    char ch = s[i--];

    // If it is an operand, return it.
    if (isOperand(ch))
        return string(1, ch);

    // For postfix, process right operand first
    // while traversing from right to left.
    string right = solve(s, i);

    // Then process the left operand.
    string left = solve(s, i);

    // Combine left and right expressions.
    return "(" + left + ch + right + ")";
}

string postToInfix(string &s)
{
    // Start from the last character.
    int i = s.size() - 1;

    // Recursively build the infix expression.
    return solve(s, i);
}

int main()
{

    string s = "abc/-";

    cout << postToInfix(s) << endl;

    return 0;
}
Java
import java.util.*;

public class GFG {
    
    // Check whether the character is an operand.
    public static boolean isOperand(char x)
    {
        return (x >= 'a' && x <= 'z')
            || (x >= 'A' && x <= 'Z');
    }

    // Recursively convert postfix expression to infix.
    public static String solve(String s, int[] i)
    {
        // Take the current character from right to left.
        char ch = s.charAt(i[0]--);

        // If it is an operand, return it.
        if (isOperand(ch))
            return Character.toString(ch);

        // For postfix, process right operand first
        // while traversing from right to left.
        String right = solve(s, i);

        // Then process the left operand.
        String left = solve(s, i);

        // Combine left and right expressions.
        return "(" + left + ch + right + ")";
    }

    public static String postToInfix(String s)
    {
        // Start from the last character.
        int[] i = { s.length() - 1 };

        // Recursively build the infix expression.
        return solve(s, i);
    }

    public static void main(String[] args)
    {
        String s = "abc/-";

        System.out.println(postToInfix(s));
    }
}
Python
def isOperand(x):
    return (x >= 'a' and x <= 'z') or (x >= 'A' and x <= 'Z')


def solve(s, i):
    
    # Take the current character from right to left.
    ch = s[i[0]]
    i[0] -= 1

    # If it is an operand, return it.
    if isOperand(ch):
        return ch

    # For postfix, process right operand first
    # while traversing from right to left.
    right = solve(s, i)

    # Then process the left operand.
    left = solve(s, i)

    # Combine left and right expressions.
    return f'({left}{ch}{right})'


def postToInfix(s):
    
    # Start from the last character.
    i = [len(s) - 1]

    # Recursively build the infix expression.
    return solve(s, i)


if __name__ == "__main__":
    s = "abc/-"

    print(postToInfix(s))
C#
using System;

public class GFG {
    
    // Check whether the character is an operand.
    public static bool IsOperand(char x)
    {
        return (x >= 'a' && x <= 'z')
            || (x >= 'A' && x <= 'Z');
    }

    // Recursively convert postfix expression to infix.
    public static string Solve(string s, ref int i)
    {
        // Take the current character from right to left.
        char ch = s[i--];

        // If it is an operand, return it.
        if (IsOperand(ch))
            return ch.ToString();

        // For postfix, process right operand first
        // while traversing from right to left.
        string right = Solve(s, ref i);

        // Then process the left operand.
        string left = Solve(s, ref i);

        // Combine left and right expressions.
        return "(" + left + ch + right + ")";
    }

    public static string postToInfix(string s)
    {
        // Start from the last character.
        int i = s.Length - 1;

        // Recursively build the infix expression.
        return Solve(s, ref i);
    }

    public static void Main()
    {
        string s = "abc/-";

        Console.WriteLine(postToInfix(s));
    }
}
JavaScript
function isOperand(x)
{
    return (x >= "a" && x <= "z") || (x >= "A" && x <= "Z");
}

function solve(s, i)
{
    // Take the current character from right to left.
    let ch = s[i[0]--];

    // If it is an operand, return it.
    if (isOperand(ch))
        return ch;

    // For postfix, process right operand first
    // while traversing from right to left.
    let right = solve(s, i);

    // Then process the left operand.
    let left = solve(s, i);

    // Combine left and right expressions.
    return "(" + left + ch + right + ")";
}

function postToInfix(s)
{
    // Start from the last character.
    let i = [ s.length - 1 ];

    // Recursively build the infix expression.
    return solve(s, i);
}

// Driver Code
let s = "abc/-";
console.log(postToInfix(s));

Output
(a-(b/c))

Using Stack - O(n) Time and O(n) Space

The idea is to traverse the postfix expression and use a stack to store operands and intermediate infix expressions.

Working of Approach:

  • Traverse the postfix expression from left to right.
  • If the character is an operand, push it into the stack.
  • If it is an operator, pop the right operand first and then the left operand.
  • Create (left operator right) and push it back.
  • The element remaining at the top is the final infix expression.

Let us understand with an example:
Input: s = "abc/-"

  • Traverse the postfix expression from left to right and push operands into the stack.
  • For /, pop c and b, form (b/c), and push it back into the stack.
  • For -, pop (b/c) and a, form (a-(b/c)), and push it back.
  • After processing all characters, the stack contains the final infix expression.
  • Output: (a-(b/c))
C++
#include <iostream>
#include <stack>
#include <string>
using namespace std;

bool isOperand(char x)
{
    return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z');
}

string postToInfix(string &postfix)
{
    stack<string> s;
    int n = postfix.size();

    // Iterating through each character in the expression.
    for (int i = 0; i < n; i++)
    {
        // If the character is an operand, push it to the stack.
        if (isOperand(postfix[i]))
        {
            string op(1, postfix[i]);
            s.push(op);
        }
        else
        {
            // If the character is an operator, pop two operands from the stack,
            // create a new string in the format (op2 operator op1), and push it
            // back to the stack.
            string op1 = s.top();
            s.pop();
            string op2 = s.top();
            s.pop();
            s.push("(" + op2 + postfix[i] + op1 + ")");
        }
    }

    return s.top();
}

int main()
{

    string s = "abc/-";

    cout << postToInfix(s) << endl;

    return 0;
}
Java
import java.util.Stack;

public class GFG {
    public static boolean isOperand(char x)
    {
        return (x >= 'a' && x <= 'z')
            || (x >= 'A' && x <= 'Z');
    }

    public static String postToInfix(String postfix)
    {
        Stack<String> s = new Stack<>();
        int n = postfix.length();

        // Iterating through each character in the
        // expression.
        for (int i = 0; i < n; i++) {
            // If the character is an operand, push it to
            // the stack.
            if (isOperand(postfix.charAt(i))) {
                String op
                    = Character.toString(postfix.charAt(i));
                s.push(op);
            }
            else {
                // If the character is an operator, pop two
                // operands from the stack, create a new
                // string in the format (op2 operator op1),
                // and push it back to the stack.
                String op1 = s.pop();
                String op2 = s.pop();
                s.push("(" + op2 + postfix.charAt(i) + op1
                       + ")");
            }
        }

        return s.pop();
    }

    public static void main(String[] args)
    {
        String s = "abc/-";
        System.out.println(postToInfix(s));
    }
}
Python
def isOperand(x):
    return ('a' <= x <= 'z') or ('A' <= x <= 'Z')

def postToInfix(postfix):
    s = []
    n = len(postfix)

    # Iterating through each character in the expression.
    for i in range(n):
        # If the character is an operand, push it to the stack.
        if isOperand(postfix[i]):
            s.append(postfix[i])
        else:
            # If the character is an operator, pop two operands from the stack,
            # create a new string in the format (op2 operator op1), and push it
            # back to the stack.
            op1 = s.pop()
            op2 = s.pop()
            s.append("(" + op2 + postfix[i] + op1 + ")")

    return s[-1]

if __name__ == "__main__":
    s = "abc/-"
    print(postToInfix(s))
C#
using System;
using System.Collections.Generic;

public class GFG {
    public static bool IsOperand(char x)
    {
        return (x >= 'a' && x <= 'z')
            || (x >= 'A' && x <= 'Z');
    }

    public static string postToInfix(string postfix)
    {
        Stack<string> s = new Stack<string>();
        int n = postfix.Length;

        // Iterating through each character in the
        // expression.
        for (int i = 0; i < n; i++) {
            // If the character is an operand, push it to
            // the stack.
            if (IsOperand(postfix[i])) {
                s.Push(postfix[i].ToString());
            }
            else {
                // If the character is an operator, pop two
                // operands from the stack, create a new
                // string in the format (op2 operator op1),
                // and push it back to the stack.
                string op1 = s.Pop();
                string op2 = s.Pop();
                s.Push("(" + op2 + postfix[i] + op1 + ")");
            }
        }

        return s.Pop();
    }

    public static void Main()
    {
        string s = "abc/-";
        Console.WriteLine(postToInfix(s));
    }
}
JavaScript
function isOperand(x)
{
    return (x >= "a" && x <= "z") || (x >= "A" && x <= "Z");
}

function postToInfix(postfix)
{
    let s = [];
    let n = postfix.length;

    // Iterating through each character in the expression.
    for (let i = 0; i < n; i++) {
        // If the character is an operand, push it to the
        // stack.
        if (isOperand(postfix[i])) {
            s.push(postfix[i]);
        }
        else {
            // If the character is an operator, pop two
            // operands from the stack, create a new string
            // in the format (op2 operator op1), and push it
            // back to the stack.
            let op1 = s.pop();
            let op2 = s.pop();
            s.push("(" + op2 + postfix[i] + op1 + ")");
        }
    }

    return s[s.length - 1];
}

// Driver Code
let s = "abc/-";
console.log(postToInfix(s));

Output
(a-(b/c))

Related Post

Comment