-
Notifications
You must be signed in to change notification settings - Fork 17
/
find-duplicate-subtrees.py
57 lines (41 loc) · 1.69 KB
/
find-duplicate-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
from typing import DefaultDict, List
from collections import defaultdict
from enum import Enum
# Definition for a binary tree node.
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]:
def check_same_tree(left: TreeNode, right: TreeNode) -> bool:
val = lambda n: n.val if n else None
if not left and not right:
return True
if val(left.left) == val(right.left) and val(left.right) == val(
right.right
):
return check_same_tree(left.left, right.left) and check_same_tree(
left.right, right.right
)
return False
nodes_map: DefaultDict[int, List[TreeNode]] = defaultdict(list)
nodes_map_check: DefaultDict[int, List[bool]] = defaultdict(list)
duplicate_trees: List[TreeNode] = []
def dfs(node: TreeNode) -> None:
matched = False
for pos in range(len(nodes_map[node.val])):
if check_same_tree(node, nodes_map[node.val][pos]):
if nodes_map_check[node.val][pos] and not matched:
duplicate_trees.append(node)
nodes_map_check[node.val][pos] = False
matched = True
if node.left:
dfs(node.left)
if node.right:
dfs(node.right)
nodes_map[node.val].append(node)
nodes_map_check[node.val].append(not matched)
dfs(root)
return duplicate_trees