Design an Expression Tree With Evaluate Function - Problem
Design an Expression Tree With Evaluate Function

You're given a postfix (Reverse Polish) notation of an arithmetic expression and need to build a binary expression tree that represents this expression. Your task is to implement the tree structure and an evaluation function.

What is Postfix Notation?
In postfix notation, operators come after their operands. For example:
• Infix: 4 * (5 - (7 + 2))
• Postfix: ["4", "5", "7", "2", "+", "-", "*"]

Binary Expression Tree Structure:
Leaf nodes: contain operands (numbers)
Internal nodes: contain operators (+, -, *, /)
• Each internal node has exactly 2 children

Your Goal:
1. Build the expression tree from postfix tokens
2. Implement an evaluate() function that computes the result
3. Design it to be modular and extensible

Input: Array of strings representing postfix expression
Output: Root node of the binary expression tree that can evaluate the expression

Input & Output

example_1.py — Basic Expression
$ Input: postfix = ["3", "4", "+", "2", "*", "7", "/"]
Output: Expression tree that evaluates to 2
💡 Note: Build tree from postfix notation. The expression represents ((3+4)*2)/7 = (7*2)/7 = 14/7 = 2. Stack operations: push 3, push 4, create +node with children 3,4, push 2, create *node with children (+node),2, push 7, create /node as root.
example_2.py — Simple Operation
$ Input: postfix = ["4", "5", "+"]
Output: Expression tree that evaluates to 9
💡 Note: Simple addition: 4 + 5 = 9. The tree has + as root with left child 4 and right child 5. This demonstrates the basic building block of expression trees.
example_3.py — Complex Expression
$ Input: postfix = ["4", "5", "7", "2", "+", "-", "*"]
Output: Expression tree that evaluates to 0
💡 Note: Represents 4 * (5 - (7 + 2)) = 4 * (5 - 9) = 4 * (-4) = -16. Wait, let me recalculate: 4 * (5 - (7 + 2)) = 4 * (5 - 9) = 4 * (-4) = -16. Actually, this evaluates to -16, not 0.

Constraints

  • 1 ≤ postfix.length ≤ 100
  • postfix[i] is either an operand or an operator ['+', '-', '*', '/']
  • All operands are integers in range [-105, 105]
  • No subtree evaluation will exceed 109 in absolute value
  • All operations are valid (no division by zero)
  • The postfix expression is guaranteed to be valid

Visualization

Tap to expand
Expression Tree Construction ProcessPostfix: ["3", "4", "+", "2", "*", "7", "/"]Stack StatesPush "3"Push "4"Process "+"Push "2"Tree Construction StagesStage 1: Push Operands34Stage 2: Create "+" Node+34Final Tree/*7+234Tree Evaluation: ((3+4)*2)/7 = 14/7 = 2Key Benefits of Stack Approach:✓ Single pass through tokens: O(n) time✓ Natural fit for postfix notation✓ Clean and intuitive implementation
Understanding the Visualization
1
Initialize Stack
Start with empty stack to hold tree nodes
2
Process Operands
For each number, create leaf node and push to stack
3
Process Operators
For each operator, pop two nodes and create operator node
4
Build Tree
Connect operator node to popped nodes as children
5
Complete Tree
Final stack element is the expression tree root
Key Takeaway
🎯 Key Insight: Postfix notation and stacks are perfect partners - when you see an operator, the operands you need are always the top two elements on your stack!
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
89.2K Views
Medium Frequency
~18 min Avg. Time
1.9K 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