Check If Two Expression Trees are Equivalent - Problem

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 (variables), and internal nodes (nodes with two children) correspond to the operators.

In this problem, we only consider the '+' operator (i.e. addition).

You are given the roots of two binary expression trees, root1 and root2. Return true if the two binary expression trees are equivalent. Otherwise, return false.

Two binary expression trees are equivalent if they evaluate to the same value regardless of what the variables are set to.

Input & Output

Example 1 — Different Structure, Same Expression
$ Input: root1 = ["+","x","y"], root2 = ["+","y","x"]
Output: true
💡 Note: Both trees represent x + y and y + x. Since addition is commutative, these expressions are equivalent regardless of variable values.
Example 2 — Different Variables
$ Input: root1 = ["+","x","x"], root2 = ["+","y","y"]
Output: false
💡 Note: First tree represents x + x (or 2x) while second represents y + y (or 2y). These are not equivalent since they depend on different variables.
Example 3 — Complex Nested Expression
$ Input: root1 = ["+",["+","x","y"],"z"], root2 = ["+","z",["+","y","x"]]
Output: true
💡 Note: Tree 1: (x + y) + z, Tree 2: z + (y + x). Both evaluate to x + y + z due to associativity and commutativity of addition.

Constraints

  • The number of nodes in both trees are in the range [1, 4000]
  • Node.val.length <= 5
  • Node.val consists of digits and lowercase English letters
  • It's guaranteed that if a node has 2 children, then it is an operator
  • It's guaranteed that if a node has 0 children, then it is an operand

Visualization

Tap to expand
Check If Two Expression Trees are Equivalent INPUT Tree 1 (root1) + x y Tree 2 (root2) + y x root1=["+","x","y"], root2=["+","y","x"] ALGORITHM (DFS) 1 DFS Tree 1 Count each variable x: +1 y: +1 2 DFS Tree 2 Subtract each count x: -1 y: -1 3 Combine Counts Final frequency map x: +1-1 = 0 y: +1-1 = 0 4 Check All Zero All counts = 0? Return true Freq Map 'x' 0 'y' 0 All zeros! FINAL RESULT Both Trees Evaluate Same Tree 1 x + y {x:1, y:1} = Tree 2 y + x {x:1, y:1} Output: true Equivalent expressions: x+y = y+x (commutative) OK Key Insight: Two expression trees with only '+' operators are equivalent if they have the same variables with the same frequencies. Use DFS to count variables in tree1 (+1) and tree2 (-1). If all counts equal zero, trees are equivalent. Time: O(n), Space: O(n) for the frequency map. TutorialsPoint - Check If Two Expression Trees are Equivalent | DFS Approach
Asked in
Google 25 Facebook 18 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~15 min Avg. Time
327 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