Create find_duplicate_subtrees.py

This commit is contained in:
bt3gl 2023-07-30 17:49:12 -07:00 committed by GitHub
parent 2e37835477
commit 402b6d3cf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
#
# Given the root of a binary tree, return all duplicate subtrees.
#
# 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
def find_duplicates(root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
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