โ† Google questions
CodingMediumReported in a candidate post, Jul 2019

Compare Expression Trees

Filed under Google

Free read 2 of 3

Determine if commutative operators (like addition and multiplication) should evaluate trees like (A + B) and (B + A) as equivalent.

The question

Given the roots of two expression trees, determine if they are structurally equivalent and represent the same mathematical expression.

Constraints

  • Nodes can be operators (+, -, *, /) or operands (numbers/variables).
  • Tree nodes contain a value and left/right children.

Examples

Input
Tree 1: (+ 2 3), Tree 2: (+ 2 3)
Output
True
Why
Both trees have the same operator and identical left and right subtrees.

Approach

  1. Determine if commutative operators (like addition and multiplication) should evaluate trees like (A + B) and (B + A) as equivalent.

Tips

  • What they are testing: Tests recursive tree traversal and handling commutative properties if applicable.
  • Common mistake: Only checking node values without recursively verifying both left and right children.

Related Google questions

Collected from public candidate posts and coding forums. Updated Nov 2026.