AlgoViz

Postfix to Infix Conversion

Easy

Left to right, bracket each join

Problem

Convert a postfix expression to infix.

In simple words

Scan left to right; pop two operands per operator and wrap them in brackets.

The idea

Push operands and, on each operator, pop two and push the bracketed infix string. Brackets around every combination guarantee the original grouping survives without needing precedence rules.

The trick

  • Second pop is the left operand.
  • Bracket every join to keep it unambiguous.

This one walks through the worked example rather than tracing the algorithm frame by frame — a full walkthrough is still to be drawn. The code and the idea below are the real solution.

0
0

Step 1 of 2. Here's the example — ab+c* Values: 0.

1/2
Optimal
timeO(n)spaceO(n)
1scan left->right; push operands2on operator: pop a,b; push '('+a+op+b+')'

Input

array
[0]

Output

answer

Check yourself

2 quick questions about this walkthrough. A wrong answer costs nothing.

Example

Input:
ab+c*
Output:
((a+b)*c)

Practice this problem:GeeksforGeeks(opens in a new tab)

Finished the walkthrough? Add it to your streak.