Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Duplicate Subtrees Detection
00:00
5 left

Duplicate Subtrees Detection

MediumPython

Problem

Given the root of a binary tree, return a list of the root values of all duplicate subtrees. Two subtrees are duplicates if they have the same structure and the same node values. Return each duplicate subtree only once, regardless of how many times it appears.

Constraints

  • 1 <= number of nodes <= 10^4
  • -200 <= node.val <= 200
  • The tree is a binary tree and may be unbalanced

Function Signature

def find_duplicate_subtrees(root):
Interviewer

Your question is Duplicate Subtrees Detection. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.