96. Unique Binary Search Trees

Input: n = 3
Output: 5
Input: n = 1
Output: 1
  • 1 <= n <= 19
  • number of unique BST for a tree with n=1 is 1
  • number of unique BST for a tree with n=2 is 2
  • number of unique BST for a tree with n=3 is
class Solution(object):
def numTrees(self, n):
dp=[0 for i in range(n+2)]
dp[0]=1
dp[1]=1
dp[2]=2
for i in range(2,n+1):
sum_=0
for j in range(0,i):
m,k=j,i-1-j
sum_+=dp[m]*dp[k]
dp[i]=sum_
return dp[n]
class Solution(object):
def numTrees(self, n):
return(math.factorial(2*n)/(math.factorial(n)*math.factorial(n+1)))

--

--

--

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Unit Testing — UIView with Nimble+Snapshot

Apache Spark’s Join Algorithms

Joins in Apache Spark: Broadcast Join

The Efficient Frontier In Python

Make All Your Data Instantly Searchable with Algolia

The Future of Machine Learning and why it looks a lot like Julia 🤖

Classes and Objects in Java

How to build RestFul webservices using Apache Camel and SpringBoot ?

The Take Home Task Paradox

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

More from Medium

Time (part 1):

Foreword

My research journey