Delete bt_find_duplicate_subtrees.py

This commit is contained in:
bt3gl 2023-08-08 14:58:11 -07:00 committed by GitHub
parent 94de93780a
commit bb47074bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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