Stack-Based Evaluation
This version uses single-digit operands with no separators. Pop the right operand before the left; division truncates toward zero.
function evaluatePostfix(exp) {
const stack = [];
for (const c of exp) {
if ('+-*/'.includes(c)) {
const b = stack.pop(),
a = stack.pop();
if (c === '+') stack.push(a + b);
else if (c === '-') stack.push(a - b);
else if (c === '*') stack.push(a * b);
else stack.push(Math.trunc(a / b));
} else stack.push(Number(c));
}
return stack[0];
}Time: O(n) | Space: O(n)
Loading discussion…
Saved in this browser only. Private to you.