Check If Two Expression Trees are Equivalent - Problem

A binary expression tree is a specialized binary tree that represents arithmetic expressions. Think of it as a mathematical formula broken down into a tree structure where:

  • Leaf nodes contain variables (like 'a', 'b', 'x')
  • Internal nodes contain operators (in this problem, only '+' for addition)

You are given the roots of two binary expression trees, root1 and root2. Your task is to determine if these trees are equivalent - meaning they would evaluate to the same result regardless of what values the variables are assigned.

Example: The expressions a + b and b + a are equivalent due to the commutative property of addition, even though their tree structures might look different.

Return true if the two expression trees are equivalent, false otherwise.

Input & Output

example_1.py โ€” Basic Equivalence
$ Input: root1 = ['+', 'a', 'b'], root2 = ['+', 'b', 'a']
โ€บ Output: true
๐Ÿ’ก Note: Both trees represent the expression a+b, which is equivalent to b+a due to the commutative property of addition. Tree 1: a+b, Tree 2: b+a โ†’ Same variables with same frequencies.
example_2.py โ€” Non-Equivalent Trees
$ Input: root1 = ['+', 'a', 'b'], root2 = ['+', 'a', 'c']
โ€บ Output: false
๐Ÿ’ก Note: Tree 1 represents a+b while Tree 2 represents a+c. These are different expressions with different variables (b vs c), so they are not equivalent.
example_3.py โ€” Complex Nested Structure
$ Input: root1 = ['+', ['+', 'a', 'b'], 'c'], root2 = ['+', 'a', ['+', 'b', 'c']]
โ€บ Output: true
๐Ÿ’ก Note: Tree 1: (a+b)+c, Tree 2: a+(b+c). Both expressions equal a+b+c due to associativity of addition. Variable frequencies are the same: a=1, b=1, c=1 in both trees.

Visualization

Tap to expand
Expression Tree Equivalence CheckTree 1: (a+b)+c++cabTree 2: a+(c+b)+a+cbCounter StepsStep 1: Tree 1a:+1, b:+1, c:+1Step 2: Tree 2a:0, b:0, c:0Variable Frequency AnalysisTree 1 traversal:Found: a(+1), b(+1), c(+1)Tree 2 traversal:Found: a(-1), b(-1), c(-1)Final ResultAll counters = 0 โ†’ Trees are EQUIVALENT! โœ“๐ŸŽฏ Key Insight: Addition is commutative & associativeOnly variable frequencies matter, not tree structure!
Understanding the Visualization
1
Initialize Counter
Create a frequency map to track variable occurrences
2
Process First Tree
Traverse tree 1, adding +1 for each variable leaf
3
Process Second Tree
Traverse tree 2, adding -1 for each variable leaf
4
Check Balance
If all counters are zero, trees have identical variable frequencies
Key Takeaway
๐ŸŽฏ Key Insight: Since addition is commutative and associative, two expression trees are equivalent if and only if they contain the same variables with the same frequencies - tree structure doesn't matter!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m)

Where n and m are the sizes of the two trees. We visit each node exactly once.

n
2n
โœ“ Linear Growth
Space Complexity
O(h + v)

Where h is the maximum height of trees (for recursion) and v is the number of unique variables

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in both trees are equal and is in the range [1, 4999].
  • Node.val.length == 1
  • Node.val is either '+' or a lowercase English letter.
  • It's guaranteed that the tree given is a valid binary expression tree.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 25
26.5K Views
Medium Frequency
~15 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