Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Unique Binary Search Trees

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Count Unique Binary Search Trees. Start with the requirements on the right.

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.

Problem

Given an integer n, return the number of structurally unique binary search trees that can be formed using the distinct values 1 through n. Two trees are considered different if their structure differs, even if they contain the same set of values. The function should return a single integer.

Constraints

  • 1 <= n <= 19
  • Values used are exactly the integers from 1 to n
  • The answer fits in a 32-bit signed integer

Function Signature

def num_trees(n):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output