Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

96. Unique Binary Search Trees

Medium61.8% Acceptance
MathDynamic ProgrammingTree
Asked by:
A
Amazon
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.

Example 1:

Input: n = 3
Output: 5

Example 2:

Input: n = 1
Output: 1

Constraints:

  • 1 <= n <= 19

Approach

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

The key insight in #96 Unique Binary Search Trees is that we are not generating trees but counting how many structurally unique BSTs can be formed using values from 1..n. Because a Binary Search Tree depends on the choice of root, each value can be considered as the root, splitting the remaining numbers into a left subtree and a right subtree.

If we choose a number i as the root, then all numbers less than i must form the left subtree and all numbers greater than i must form the right subtree. The total number of trees for that root becomes the product of possibilities from the left and right sides. Summing this for all possible roots leads to a classic dynamic programming recurrence.

This pattern forms the well‑known Catalan number sequence. Using DP, we build results from smaller subtree sizes up to n. The approach runs in O(n²) time due to the nested root choices and uses O(n) space for the DP array.

Complexity

ApproachTime ComplexitySpace Complexity
Dynamic Programming (Catalan Recurrence)O(n^2)O(n)
Mathematical Catalan FormulaO(n)O(1)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Dynamic Programming

The problem can be efficiently solved using Dynamic Programming. We notice that the number of unique BSTs can be determined as follows: for each node i from 1 to n, calculate the number of left and right subtrees which can form by considering i as the root. We can store these results in a table to avoid redundant work. The relationship can be expressed as:

G(n) = \sum_{i=1}^{n} (G(i-1) * G(n-i))

where G(0) = 1 and G(1) = 1 are base cases.

Time Complexity: O(n^2)
Space Complexity: O(n)

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3int numTrees(int n) {
4    int dp[n + 1];
5    dp[0] 

Explanation

This C solution uses a dynamic programming array dp to store the number of unique BSTs for each number of nodes up to n. We initialize dp[0] and dp[1] to 1 as base cases. Then, for each number i, the number of unique BSTs can be calculated using previous results.

Catalan Number Formula

Using the Catalan number formula provides a mathematical way to solve the problem directly without iteration for up to moderate n. The nth catalan number C(n) is given by:

C(n) = \frac{1}{n+1}\binom{2n}{n}

The C(n) represents the number of unique BSTs for n nodes. This can be computed efficiently using mathematical operations.

Time Complexity: O(n)
Space Complexity: O(1)

CC++JavaPythonC#JavaScript
1


Video Solutions

Watch expert explanations and walkthroughs

Unique Binary Search Trees - Leetcode 96 - Python Dynamic Programming

NeetCode
11:2077,933 views

Asked By Companies

1 companies
A
Amazon

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Unique PathsMedium
Climbing StairsEasy
Number of Digit OneHard
Different Ways to Add ParenthesesMedium
More similar problems

Related Topics

MathDynamic ProgrammingTreeBinary Search TreeBinary Tree

Problem Stats

Acceptance Rate61.8%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Unique Binary Search Trees asked in FAANG interviews?

Yes, this problem or its variations frequently appear in FAANG-style interviews. Interviewers use it to test understanding of dynamic programming, recursion patterns, and tree-related combinatorics.

What is the optimal approach for Unique Binary Search Trees?

The optimal approach uses dynamic programming based on the Catalan number recurrence. For each possible root, the number of unique BSTs equals the product of possible left and right subtree combinations. Summing these for all roots from 1 to n gives the final count.

Why is the Unique Binary Search Trees problem related to Catalan numbers?

The recurrence formed by splitting nodes into left and right subtrees matches the Catalan number formula. Each root divides the remaining nodes into two independent subproblems whose combinations multiply together. This structure naturally produces the Catalan sequence.

What data structure or concept is most important for solving Unique Binary Search Trees?

Dynamic programming is the most important concept because the problem builds solutions for larger n from previously computed subtree counts. Understanding BST properties and combinatorics also helps recognize the Catalan pattern.

Previous Problem

Gray Code

Next Problem

Max Points on a Line

=
dp
[
1
]
=
1
;
6
for
(
int
i
=
2
;
i
<=
n
;
i
++
)
{
7
dp
[
i
]
=
0
;
8
for
(
int
j
=
1
;
j
<=
i
;
j
++
)
{
9
dp
[
i
]
+=
dp
[
j
-
1
]
*
dp
[
i
-
j
]
;
10
}
11
}
12
return
dp
[
n
]
;
13
}
14
15
int
main
(
)
{
16
int
n
=
3
;
17
printf
(
"%d\n"
,
numTrees
(
n
)
)
;
// Output: 5
18
return
0
;
19
}
#
include
<stdio.h>
2
3
long
long
binomialCoeff
(
int
n
,
int
k
)
{
4
long
long
res
=
1
;
5
if
(
k
>
n
-
k
)
6
k
=
n
-
k
;
7
for
(
int
i
=
0
;
i
<
k
;
++
i
)
{
8
res
*=
(
n
-
i
)
;
9
res
/=
(
i
+
1
)
;
10
}
11
return
res
;
12
}
13
14
int
numTrees
(
int
n
)
{
15
long
long
c
=
binomialCoeff
(
2
*
n
,
n
)
;
16
return
(
int
)
(
c
/
(
n
+
1
)
)
;
17
}
18
19
int
main
(
)
{
20
int
n
=
3
;
21
printf
(
"%d\n"
,
numTrees
(
n
)
)
;
// Output: 5
22
return
0
;
23
}

Explanation

This C solution uses the Catalan number formula leveraging combination calculations. The function binomialCoeff computes C(2n, n) before dividing by n+1 to get the nth Catalan number.