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

Launch your own dropshipping store, for free.

CF Camp 2018: Madness in Munich Part Two- Building APIs with CFFractal & ColdBox

CCS County Badge

Ready, Steady, Go.

Guest Post: APIC Gateway delayed analytics data [Craig S]

Why bother with ta-lib?

RSpec, Factory Girl, and Ruby: a Primer in Rails Test Performance

LeetCode: The Climbing Stairs Problem

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

The Grace Hopper Celebration

Maturing

Testing the Metaverse and Beyond

Finally, I did it