# 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:
return self.dfs(root1) == self.dfs(root2)
#### 统计树rt各个字母的个数
def dfs(self, rt: 'Node') -> dict():
## 空结点
res = defaultdict(int)
if rt == None:
return res
## 叶结点
if rt.left == None and rt.right == None:
res[rt.val] = 1
return res
## 非叶结点
L = self.dfs(rt.left)
R = self.dfs(rt.right)
for char,cnt in L.items():
res[char] += cnt
for char,cnt in R.items():
res[char] += cnt
return res
# 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:
return self.dfs(root1) == self.dfs(root2)
#### 统计树rt各个字母的个数
def dfs(self, rt: 'Node') -> List[int]:
## 空结点
if rt == None:
return [0 for _ in range(26)]
## 叶结点
if rt.left == None and rt.right == None:
res = [0 for _ in range(26)]
idx = ord(rt.val) - ord('a')
res[idx] = 1
return res
## 非叶结点
L = self.dfs(rt.left)
R = self.dfs(rt.right)
return [ L[i]+R[i] for i in range(26)]