Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6420
@blackshadows 6380
@mb1973 5704
@Quandray 5245
@akhayrutdinov 5111
@saiujwal13083 5046
@sanjay05 3762
@kirtidee18 3673
@marius_valentin_dragoi 3523
@mantu_singh 3510
@sushant_a 3459
Complete Leaderboard
Unique BST's
Medium Accuracy: 44.17% Submissions: 24442 Points: 4

Given an integer. Find how many structurally unique binary search trees are there that stores the values from 1 to that integer (inclusive). 

Example 1:

Input:
N = 2
Output: 2
Explanation:for N = 2, there are 2 unique
BSTs
     1               2  
      \            /
       2         1

Example 2:

Input:
N = 3
Output: 5
Explanation: for N = 3, there are 5
possible BSTs
  1           3     3       2     1
    \        /     /      /  \     \
     3      2     1      1    3     2
    /      /       \                 \
   2      1         2                 3

 

Your Task:
You don't need to read input or print anything. Your task is to complete the function numTrees() which takes the integer N as input and returns the total number of Binary Search Trees possible with keys [1.....N] inclusive. Since the answer can be very large, return the answer modulo 1000000007.

Expected Time Complexity: O(N2).
Expected Auxiliary Space: O(N).

Constraints:
1<=N<=1000

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Unique BST's

Output Window