Skip to content

Commit a01658a

Browse files
authored
Update find-duplicate-subtrees.py
1 parent 597e474 commit a01658a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Python/find-duplicate-subtrees.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ def findDuplicateSubtrees(self, root):
3535
:type root: TreeNode
3636
:rtype: List[TreeNode]
3737
"""
38-
def getid(root, result):
38+
def getid(root, lookup, trees):
3939
if root:
40-
id = lookup[root.val, getid(root.left, result), getid(root.right, result)]
41-
result[id].append(root)
42-
return id
43-
result = collections.defaultdict(list)
40+
node_id = lookup[root.val, \
41+
getid(root.left, lookup, trees), \
42+
getid(root.right, lookup, trees)]
43+
trees[node_id].append(root)
44+
return node_id
45+
trees = collections.defaultdict(list)
4446
lookup = collections.defaultdict()
4547
lookup.default_factory = lookup.__len__
46-
getid(root, result)
47-
return [roots[0] for roots in result.values() if len(roots) > 1]
48+
getid(root, lookup, trees)
49+
return [roots[0] for roots in trees.values() if len(roots) > 1]
4850

4951

5052
# Time: O(n * h)

0 commit comments

Comments
 (0)