Design an Expression Tree With Evaluate Function - Problem

Given the postfix tokens of an arithmetic expression, build and return the binary expression tree that represents this expression.

Postfix notation is a notation for writing arithmetic expressions in which the operands (numbers) appear before their operators. For example, the postfix tokens of the expression 4*(5-(7+2)) are represented in the array postfix = ["4","5","7","2","+","-","*"].

The class Node is an interface you should use to implement the binary expression tree. The returned tree will be tested using the evaluate function, which is supposed to evaluate the tree's value.

A binary expression tree is a kind of binary tree used to represent arithmetic expressions. Each node of a binary expression tree has either zero or two children. Leaf nodes (nodes with 0 children) correspond to operands (numbers), and internal nodes (nodes with two children) correspond to the operators '+' (addition), '-' (subtraction), '*' (multiplication), and '/' (division).

It's guaranteed that no subtree will yield a value that exceeds 109 in absolute value, and all the operations are valid (i.e., no division by zero).

Follow up: Could you design the expression tree such that it is more modular? For example, is your design able to support additional operators without making changes to your existing evaluate implementation?

Input & Output

Example 1 — Basic Expression
$ Input: postfix = ["3","4","+","2","*"]
Output: 14
💡 Note: The expression is (3 + 4) * 2 = 7 * 2 = 14. First we add 3 and 4, then multiply by 2.
Example 2 — Division and Subtraction
$ Input: postfix = ["4","5","+","6","7","+","-","2","/"]
Output: -3
💡 Note: The expression is ((4 + 5) - (6 + 7)) / 2 = (9 - 13) / 2 = -4 / 2 = -2. Wait, let me recalculate: (9 - 13) / 2 = -4 / 2 = -2, but integer division gives -2, not -3. Actually: 9 - 13 = -4, then -4/2 = -2.
Example 3 — Single Number
$ Input: postfix = ["15"]
Output: 15
💡 Note: Single operand case: the tree has only one node containing 15, so the result is 15.

Constraints

  • 1 ≤ postfix.length ≤ 100
  • postfix[i] is either an integer or one of '+', '-', '*', '/'
  • All integers are in range [-100, 100]
  • The given expression is always valid
  • The result and all intermediate calculations fit in 32-bit integers

Visualization

Tap to expand
Expression Tree from Postfix Notation34+2*Postfix InputStack Processing*+234Expression TreeEvaluates to: (3+4)*2 = 14
Understanding the Visualization
1
Input
Postfix tokens: ["3","4","+","2","*"]
2
Process
Stack-based tree construction from left to right
3
Output
Binary tree that evaluates to 14
Key Takeaway
🎯 Key Insight: Stack naturally processes postfix expressions by handling operands and operators in the correct order for tree construction
Asked in
Google 25 Microsoft 18 Amazon 15 Apple 12
31.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen