Check If Two Expression Trees are Equivalent Problem
Description
LeetCode Problem 1612.
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.
Example 1:
Input: root1 = [x], root2 = [x]
Output: true
Example 2:

Input: root1 = [+,a,+,null,null,b,c], root2 = [+,+,a,b,c]
Output: true
Explanation: a + (b + c) == (b + c) + a
Example 3:

Input: root1 = [+,a,+,null,null,b,c], root2 = [+,+,a,b,d]
Output: false
Explanation: a + (b + c) != (b + d) + a
Constraints:
- The number of nodes in both trees are equal, odd and, in the range
[1, 4999]. Node.valis'+'or a lower-case English letter.- It's guaranteed that the tree given is a valid binary expression tree.
Follow up: What will you change in your solution if the tree also supports the '-' operator (i.e. subtraction)?
Solution
Approach: Count the Variables
With only the + operator, a tree's value is just the sum of its leaves, in some grouping and order. Addition is commutative and associative, so the grouping and order don't matter. Two trees always have the same value exactly when each variable appears the same number of times in both. (If some variable appeared a different number of times, setting it to 1 and every other variable to 0 would give different values.)
So keep one array count of 26 counters. Traverse the first tree and add 1 for each leaf letter; traverse the second tree and subtract 1 for each leaf. The trees are equivalent if all counters end at 0. + nodes are internal nodes and are skipped.
Follow up (subtraction): a variable on the right side of a - counts with the opposite sign. Pass a sign down the traversal: the right child of a - node flips it. Then compare the signed counts the same way.
Complexity: O(n) time, and O(h) extra space for the recursion, where h is the height of the trees.
# Definition for a binary tree node.
# class Node(object):
# def __init__(self, val=" ", left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
# With only '+', a tree's value is the sum of its leaves, so two trees are equivalent
# exactly when every variable appears the same number of times in both.
count = [0] * 26
def dfs(node, sign):
if node is None:
return
if node.val != "+":
count[ord(node.val) - ord("a")] += sign
dfs(node.left, sign)
dfs(node.right, sign)
dfs(root1, 1)
dfs(root2, -1)
return all(c == 0 for c in count)