Postfix Evaluator

Stack Data Structures Postfix

Postfix Expression Evaluator

Given a string representing a valid postfix expression (also known as Reverse Polish Notation) with integers and the operators +, -, *, and / (each token separated by a single space), write a function to evaluate the expression and return the result as an integer.

Important Details:

  • The expression is guaranteed to be valid.
  • Division should truncate towards zero.
  • You can assume that there will be no division by zero.

Example:

  • Input: "2 1 + 3 *"
  • Output: 9

Explanation: The expression is evaluated as (2 + 1) * 3 = 9.

Hint: Use a stack to store intermediate results while processing the expression from left to right.

Happy coding on this fine January day!