-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathok_find_dublicate_subtrees.py
64 lines (48 loc) · 1.29 KB
/
ok_find_dublicate_subtrees.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
652. Find Duplicate Subtrees
Medium
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees,
you only need to return the root node of any one of them.
Two trees are duplicate if they have the same structure with same node values.
Example 1:
1
/ \
2 3
/ / \
4 2 4
/
4
The following are two duplicate subtrees:
2
/
4
and
4
Therefore, you need to return above trees' root in the form of a list.
168 / 168 test cases passed.
Status: Accepted
Runtime: 56 ms
Memory Usage: 20.2 MB
"""
from collections import defaultdict
from typing import List
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def findDuplicateSubtrees(self, root: TreeNode) -> List[TreeNode]:
uid_count = defaultdict(int)
output = []
def dfs_subtree(node):
if node:
triplet = ' '.join((str(node.val), dfs_subtree(node.left), dfs_subtree(node.right)))
uid_count[triplet] += 1
if uid_count[triplet] == 2:
output.append(node)
return triplet
else:
return ''
dfs_subtree(root)
return output