"Leetcode" 96. Unique binary Search trees-the number of the only two-pronged sort tree

Source: Internet
Author: User

First, Description:

Second, the idea:

BST (binary sort tree): The result of the middle sequence traversal is a non-descending sequence, and the results of the middle sequence traversal of the different binary trees with the same node (number and value) are the same;

When the number of nodes of Zuozi is determined, the number of right subtree is also determined;

When the number of nodes is 0 or 1 o'clock, there are only 1 binary trees, expressed as f (0) =1,f (1) =f (0) *f (0);

When the number of nodes is 2 o'clock, the total number of Species = left subtree is empty f (0) * Right subtree is not empty f (1) + left subtree is not empty f (1) * Right subtree is empty f (0), i.e. f (0) *f (1) +f (1) *f (0) = 2 species;

When the number of nodes is 3 o'clock, there is a left dial hand tree is empty f (0) * Right subtree is not empty f (2) + left subtree is not empty f (2) * Right subtree is empty f (0) + left subtree is not empty f (1) *f (1), i.e. f (0) *f (2) +f (2) *f (0) +f (1) *f (1) =1*2+2*1+1 *1=5 species;

......

When the number of nodes is n, the result is f (0) *f (n-1) +f (1) *f (n-2) +......+f (n-2) *f (1) +f (n-1) *f (0);

From the above law can be obtained, when the number of nodes n is greater than 0 o'clock, n corresponding to the different binary tree species is always dependent on n equals 0-(n-1) when the corresponding value is obtained, it can be 0-(n-1) value corresponding to the number of the two-fork tree is saved to a hashmap<k,v> collection, Recursive invocation only need to get the corresponding value from the collection;

Have you ever felt like the Fibonacci sequence, the same principle.

Third, the code:
 Public classSolution { Public intNumtrees (intN) {if(N==0 | | n==1){            return1; } Map<integer, integer> map =NewHashmap<integer, integer>(); Map.put (0, 1); Map.put (1, 1);  for(inti=2;i<=n;i++){            intCount = 0;  for(intj=0;j<i;j++) {Count+ = Map.get (j) *map.get (i-j-1);//minus 1 is in addition to the root node.} map.put (I, count); }        returnMap.get (n);//n the number of trees corresponding to    }}

"Leetcode" 96. Unique binary Search trees-the number of the only two-pronged sort tree

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.