mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Delete bt_find_duplicate_subtrees.py
This commit is contained in:
parent
94de93780a
commit
bb47074bdd
@ -1,32 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# author: bt3gl
|
|
||||||
|
|
||||||
# Given the root of a binary tree, return all duplicate subtrees.
|
|
||||||
|
|
||||||
|
|
||||||
def find_duplicates(root: Optional[Node]) -> list[Optional[Node]]:
|
|
||||||
|
|
||||||
result = []
|
|
||||||
counter = {}
|
|
||||||
|
|
||||||
def traverse(node):
|
|
||||||
if not node:
|
|
||||||
return ""
|
|
||||||
|
|
||||||
rep = ("(" + traverse(node.left) + ")" + \
|
|
||||||
str(node.val) + "(" + \
|
|
||||||
traverse(node.right) + ")")
|
|
||||||
|
|
||||||
if rep in counter:
|
|
||||||
counter[rep] += 1
|
|
||||||
else:
|
|
||||||
counter[rep] = 1
|
|
||||||
|
|
||||||
if counter[rep] == 2:
|
|
||||||
result.append(node)
|
|
||||||
|
|
||||||
return rep
|
|
||||||
|
|
||||||
traverse(root)
|
|
||||||
return result
|
|
Loading…
x
Reference in New Issue
Block a user