mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 14:56:27 -04:00
Delete bt_find_duplicate_subtrees.py
This commit is contained in:
parent
94de93780a
commit
bb47074bdd
1 changed files with 0 additions and 32 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue