Build Binary Expression Tree From Infix Expression - Problem
Build Binary Expression Tree From Infix Expression

Imagine you're a compiler engineer tasked with converting mathematical expressions into a tree structure for efficient evaluation! 🌳

A binary expression tree is a special kind of binary tree where:
  • Leaf nodes contain operands (numbers)
  • Internal nodes contain operators (+, -, *, /)
  • Each internal node has exactly two children

Given an infix expression string s containing operands, operators, and parentheses, your goal is to construct a valid binary expression tree that represents this expression.

Key Rules:
  • 🎯 Order of operations matters: parentheses first, then *//, then +/-
  • 📖 In-order traversal of your tree should reproduce the original expression (without parentheses)
  • 🔢 Operands must appear in the same order in both input and tree traversal

For example: "2*3/(2-1)+1" should produce a tree that evaluates correctly following mathematical precedence rules!

Input & Output

example_1.py — Basic Addition
$ Input: s = "2+3"
Output: TreeNode('+', TreeNode('2'), TreeNode('3'))
💡 Note: Simple addition creates a tree with '+' as root and '2', '3' as children. In-order traversal gives '2+3'.
example_2.py — Precedence Example
$ Input: s = "2*3+1"
Output: TreeNode('+', TreeNode('*', TreeNode('2'), TreeNode('3')), TreeNode('1'))
💡 Note: Multiplication has higher precedence than addition, so '2*3' forms the left subtree, then '+' combines it with '1'.
example_3.py — Parentheses Override
$ Input: s = "2*(3+1)"
Output: TreeNode('*', TreeNode('2'), TreeNode('+', TreeNode('3'), TreeNode('1')))
💡 Note: Parentheses force '3+1' to be evaluated first, making it the right subtree of the multiplication.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of digits, '+', '-', '*', '/', '(', and ')'
  • Operands are positive integers
  • The expression is guaranteed to be valid
  • No leading zeros in operands

Visualization

Tap to expand
Binary Expression Tree from Infix INPUT "2*3/(2-1)+1" Tokens: 2 * 3 / ( 2 - 1 ) + 1 Operand Operator Parens Operator Precedence: 1. Parentheses ( ) 2. Multiply/Divide * / 3. Add/Subtract + - Simple Example: s = "2+3" ALGORITHM STEPS 1 Parse Tokens Split into operands, operators 2 Use Two Stacks Operand stack + Operator stack Operands Node Node Operators + * 3 Apply Precedence Pop when higher/equal priority 4 Build Subtrees Connect operator with operands + 2 3 FINAL RESULT + / 1 * - 2 3 2 1 In-order Traversal: 2 * 3 / ( 2 - 1 ) + 1 Simple "2+3" Output: TreeNode('+', Node('2'), Node('3')) Key Insight: The algorithm uses the Shunting-Yard approach: when encountering an operator, pop and build subtrees for all operators with higher or equal precedence before pushing the new one. Parentheses force immediate evaluation of their contents. Time: O(n) | Space: O(n) for the stacks. TutorialsPoint - Build Binary Expression Tree From Infix Expression | Optimal Solution
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
24.4K Views
Medium-High 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