Leetcode 96:unique Binary Search Trees java

Source: Internet
Author: User

96:given N, how many structurally unique BST's (binary search trees) that store values 1 ... n?

For example,
Given N = 3, there is a total of 5 unique BST ' s.

1         3     3      2      1    \//      /\           3     2     1      1   3      2    /     /       \                    2     1         2                 3

Check the next, this question to use Cattleya number reference this blog also did not see understand: http://www.cnblogs.com/springfor/p/3884009.html
And then saw a video of three elder brother: Https://www.youtube.com/watch?v=UfA_v0VmiDg

So put aside Cattleya number, big idea is dynamic programming. Find the recursive formula.
The idea is: Anyway is BST, each element is done root node, calculate each element to do root node when there are several cases, and then each node of several cases Add.
Each element has several cases, according to the characteristics of BST, is the order of convenience is ordered. So:
Take n=3 as an example. Create a new int res[]= int[3+1] (round-robin, each number corresponds to the index, not the 1 corresponding to the 0index,2 corresponding to the 1 index ...)
1 is the root node, 1 of the Zuozi is 0, the right subtree is 2,3 (two). So is 1 is the number of types of root nodes: Res[0] times res[2] number of cases.
When 2 is the root node, 2 of the Zuozi are 1 (1) and the right subtree is 1 (1). So is 2 is the number of types of root nodes: res[1] times res[1] number of cases.
3 is the root node, 3 of the Zuozi is (2), the right subtree is (0). So is 2 is the number of types of root nodes: res[2] times res[0] number of cases.

1  Public classSolution {2      Public intNumtrees (intN) {3         if(N ==0 | | n ==1)return1;4         intRes[] =New int[N+1];5Res[0]=1;6          for(inti = 1; I <= N; i++){7              for(intj = 0; J < I; J + +){8Res[i] + = Res[j]*res[i-j-1];9              }Ten          } One         returnRes[n]; A     } -}

Res[0] is the time when there are 0 numbers.

RES[1] is the time when there are 1 numbers.

。。。。

Leetcode 96:unique Binary Search Trees java

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.