-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path652.py
36 lines (28 loc) · 981 Bytes
/
652.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
from collections import defaultdict
from typing import List
# Name: Find Duplicate Subtrees
# Link: https://leetcode.com/problems/find-duplicate-subtrees/submissions/
# Method: Solving tidbit
# Time: O(n^2)
# Space: O(n^2)
# Difficulty: Medium
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def findDuplicateSubtrees(self, root: TreeNode) -> List[TreeNode]:
node_repr = defaultdict(list)
def stringify_tree(node: TreeNode):
if not node:
return "null"
left_res = stringify_tree(node.left)
right_res = stringify_tree(node.right)
res = f"{node.val},{left_res},{right_res}"
node_repr[res].append(node)
return res
stringify_tree(root)
return [
tree_roots[0] for tree_roots in node_repr.values() if len(tree_roots) > 1
]