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","+","2","*"]
Output: 18
💡 Note: The expression is (4 + 5) * 2 = 9 * 2 = 18. First we add 4 and 5 to get 9, then multiply by 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 With Evaluate Function INPUT Postfix Tokens Array: "3" "4" "+" "2" "*" 0 1 2 3 4 Stack-based Building: Use stack to build tree operands --> push operators --> pop 2 create node push result node ALGORITHM STEPS 1 Process "3", "4" Push as leaf nodes 3 4 2 Process "+" Pop 3,4 create subtree + 3 4 3 Process "2" Push as leaf node 4 Process "*" Pop subtree and 2 Create final tree evaluate(): recursive DFS (3+4)*2 = 7*2 = 14 FINAL RESULT Expression Tree: * + 2 3 4 Evaluate Recursively: left = 3 + 4 = 7 right = 2 root = 7 * 2 = 14 Output: 14 Operand Operator Key Insight: Postfix notation naturally maps to stack-based tree construction. Operands become leaf nodes, operators pop two nodes and create internal nodes. The evaluate() function uses recursive DFS: leaf nodes return their value, operators recursively evaluate children then apply the operation. TutorialsPoint - Design an Expression Tree With Evaluate Function | Optimal Solution Time: O(n) | Space: O(n) for stack and tree
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