-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path652.py
More file actions
24 lines (21 loc) · 645 Bytes
/
Copy path652.py
File metadata and controls
24 lines (21 loc) · 645 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def findDuplicateSubtrees(self, root):
"""
:type root: TreeNode
:rtype: List[TreeNode]
"""
if not root:
return []
tree_list = []
cnt = collections.Counter()
def Traverse(node):
if not node:
return '#'
serialized_tree = "{}{}{}".format(
node.val, Traverse(node.left), Traverse(node.right))
cnt[serialized_tree] += 1
if cnt[serialized_tree] == 2:
tree_list.append(node)
return serialized_tree
Traverse(root)
return tree_list