96. Unique Binary Search Trees
Contents
Problem
Given n, how many structurally unique (BST) that store values 1 … n?
example 1
|
|
Solution
Dynamic programming
Main idea - dynamic programming :
We know that unique bst for n=1 is 1, for n=0 is 1, for n=2 is 2.
Also, we know than for n=3 : {1,2,3} we can check tries with:
- root 1 – its mean that we should check tries right with nodes
{2,3} - root 2 – its mean that we should check tries left with nodes
{2}and right with nodes{3} - root 3 – its mean that we should check tries left with nodes
{1,2}
|
|