Write code to evaluate a given postfix expression efficiently.

For example,

82/ will evaluate to 4 (8/2)
 
138*+ will evaluate to 25 (1+8*3)
 
545*+5/ will evaluate to 5 ((5+4*5)/5)

Assume that the postfix expression contains only single-digit numeric operands, without any whitespace.

Practice this problem

We can easily compute a postfix expression by using a stack. The idea is to traverse the given postfix expression from left to right. If the current character of the expression is an operand, push it into the stack; otherwise, if the current character is an operator, pop the top two elements from the stack, evaluate them using the current operator and push the result back into the stack. When all the expression characters are processed, we will be left with only one element in the stack containing the value of a postfix expression.

Following is the implementation in C++, Java, and Python based on the above idea:

C++


Download  Run Code

Output:

25

Java


Download  Run Code

Output:

25

Python


Download  Run Code

Output:

25

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the length of the postfix expression.