Leet Code--Unique BST

Source: Internet
Author: User

for number n (greater than 1), how many binary search tree (BST sequences) from 1 to n?
When n=3, the BST sequence is:


   1         3     3    2     1      \        /   /     /\      \
&nb Sp    3      2    1    1  3     2
     /            \                  \
   2      1         2                3
A total of 5.


Parse:


N=1, the BST sequence is
 1
 /  \
      NULL  null
1 kinds


n=2, the BST sequence is
1 & nbsp      2
 \        /

2 1


2 kinds


N=3, the BST sequence is listed as
1 3 3) 2 1
\         /     /       / \       \
3 2 1 1 3 2
/       /        \                    \
2 1 2 3
5 kinds


N=4, the BST sequence is listed as
1 4 2 3
\             +                                        /                 +    / \                             +          / \
2,3,4 (5 kinds) Three-way (5 kinds) (1 kinds) 1 3,4 (2 kinds) (2 kinds) 1, 2 4 (1 kinds)
Total 5+5+1*2+2*1 = 14 kinds


N=5, the BST sequence is listed as

   1                     &NBS P                        2           &N Bsp                          ,         &NB Sp  3                                 &N Bsp                   4                 & nbsp                   

     \                                                                                ,         &N Bsp  / \                                                  / \             & nbsp                      

2,3,4,5 (14 kinds) (1 kinds) 1 3,4,5 (5 kinds) (2 kinds) 4,5 (2 kinds) (5 kinds) 5 (1 kinds)


5

/

1,2,3,4 (14 kinds)

Therefore, count (5) = 14 + 1*5 + 2*2 + 5*1 + 14 = 42 kinds


There appears to be a recursive relationship, considering the DP solution.
To find the law, to seek the formula of recursion:
Set S (n) to n corresponding case number, s (0) =1, then,
S (1) = 1
S (2) = 2
S (3) = s (0) * S (2) + S (1) * s (1) + S (2) * S (0) = 5
S (4) = s (0) * S (3) + S (1) * s (2) + S (2) * s (1) + S (3) * S (0) = 14


Not hard to find,
S (N) = Sum{s (K-1) * S (n-k), where K∈[1,n]}


Given the recursive formula, the next step is to write the code:




public class Solution {public    int numtrees (int n) {        if (n <= 0) {return 1;}  -DP Arrayvar DP = new Int[n+1];DP [0] = 1;dp[1] = 1;for (var j = 2; J <= N; j + +) {//I:1: j//dp[j] = SUM (dp[i-1] * Dp[j-i]) var s = 0;for (var i = 1; I <= J; i++) {s + = dp[i-1] * dp[j-i];} DP[J] = s;} return dp[n];}    }


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leet Code--Unique BST

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.