Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Duplicate Subtrees Detection

MediumPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Duplicate Subtrees Detection. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

You need to log in / sign up to run or submit.

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output