N nodes, two forked trees of different shapes (number + spawn)

Source: Internet
Author: User
Tags lintcode

Topic Links:

Different two-fork find tree: http://www.lintcode.com/zh-cn/problem/unique-binary-search-trees/

different two-fork find tree II: http://www.lintcode.com/zh-cn/problem/unique-binary-search-trees-ii/

Number of binary trees of different morphology:Sample Example

Given n = 3, there are 5 different forms of two-fork lookup trees:

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

Can be analyzed, when the n=1, only 1 root nodes, it can only constitute 1 forms of two-fork tree, so that n nodes can be composed of two of the number of trees represented as h (n), then H (1) = 1; H (0) = 0;

When n=2, 1 root nodes are fixed, and there are 2-1 nodes. This node can be divided into (1,0), (0,1) two groups. Put 1 on the left, 0 on the right, or 0 on the left and 1 on the right. namely: H (2) =h (0) *h (1) +h (1) *h (0) = 2, it can be composed of 2 species of two-fork tree.

When n=3, 1 root nodes are fixed, and there are 2 nodes. These 2 nodes can be divided into (2,0), (0,2), (3) groups. That is, H (3) =h (0) *h (2) +h (1) *h (1) +h (2) *h (0) = 5, it is possible to form 5-branched trees.

And so on, when n>=2, can be composed of two fork tree number is H (n) =h (0) *h (n-1) +h (1) *h (n-2) +...+h (n-1) *h (0), that is, the definition of Catalan number, can be directly using the formula to derive results.

Make the H (1) =1,h (0) =1,catalan number (Cattleya number) satisfy the recursive formula:

  H (N) = h (0) *h (n-1) +h (1) *h (n-2) + ... + h (n-1) H (0) (where n>=2)

Alternative recursion Type:

h (N) = ((4*n-2)/(n+1)) *h (n-1);

The solution of this recursive relationship is:

  H (N) =c (2n,n)/(n+1) (n=1,2,3,...)

from this thought of the last said "N number sequentially into the stack, the number of stack order?" " the same is the Cattleya number.

Http://www.cnblogs.com/hujunzheng/p/4845354.html

Code
classSolution { Public:    /** * @paramn n:an integer * @return: An integer*/    Long LongCintNintm) {N= n-m+1; Long LongAns =1;  for(intI=1; i<=m; ++i) {ans*= n++; Ans/=i; }        returnans; }    intNumtrees (intN) {//Write your code here        returnC2*n, N)/(n+1); }};

Constructing a binary tree of different forms: a sample

Give n = 3 to generate all 5 different patterns of the two-fork lookup tree:

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

In fact, through the example, we can find that n nodes construct different morphological binary tree processes, 1,2,3.....N nodes, enumerate each node as root node (assuming root, 1<=root<=n), then (1,2..root-1) and (Root+1, ROOT+2...N) is the left and right subtree of root, respectively. Every step of the way repeats the process, eventually resulting in a two-fork tree of all shapes.

Algorithm implementation

First weak weak to say the implementation of their own mistakes, because the recursive implementation of the time will get different two-fork tree, then how to determine the N nodes just generated two fork tree? A variable Usenode (=0) is used to indicate how many nodes are currently used. When Usenode equals N, it shows a tree that meets the requirements, then copies the tree that was just generated, and then puts it into the vector, continuing to build the next two-fork tree that fits the criteria.

Error code:
/** Definition of TreeNode: * Class TreeNode {* Public: * int val; * TreeNode *left, *right; * TreeNode (i NT val) {* This->val = val; * This->left = This->right = NULL; *} *}*/classSolution { Public:    /** * @paramn n:an integer * @return: A List of root*/Vector<treenode *>ans; intCntnode=0;//total number of nodesTreeNode *curroot =NULL; voidCOPYT (TreeNode * &tmp, TreeNode *T) {        if(T) {tmp=NewTreeNode (t->val); COPYT (TMP->left, t->Left ); COPYT (TMP->right, t->Right ); }    }        voidBuildt (TreeNode * &t,intLdintRdintUsenode) {        if(LD > RD)return;  for(intRoot=ld; root<=rd; ++root) {T=NewTreeNode (root); if(ld==1&& rd==cntnode) Curroot=T; if(usenode+1==cntnode) {//This tree has been built, copy it.TreeNode *tmp =NULL;                COPYT (TMP, curroot);            Ans.push_back (TMP); } Buildt (T->left, LD, root-1, usenode+1); Buildt (T->right, root+1, RD, Usenode+root-ld+1); }} vector<treenode *> Generatetrees (intN) {//Write your code hereCntnode =N; TreeNode*t =NULL; Buildt (T,1N0); if(n = =0) Ans.push_back (T); returnans; }};

Later, after running, see the wrong answer compared to the correct answer, as follows:

  When the n=4

Output

[{1,#,2,#,3,#,4},{1,#,2,#,4,3},{1,#,3,2,4},{1,#,4,2,#,#,3},{1,#,4,3,#,2},{2,1,3,#,#,#,4},{2,1,4,#,#,3},{ 3,2,4,1},{4,1,#,#,2,#,3},{4,1,#,#,3,2},{4,2,#,1,3},{4,3,#,1,#,#,2},{4,3,#,2,#,1}]

Expected answers

[{1,#,2,#,3,#,4},{1,#,2,#,4,3},{1,#,3,2,4},{1,#,4,2,#,#,3},{1,#,4,3,#,2},{2,1,3,#,#,#,4},{2,1,4,#,#,3},{3,1,4, #,2},{3,2,4,1},{4,1,#,#,2,#,3},{4,1,#,#,3,2},{4,2,#,1,3},{4,3,#,1,#,#,2},{4,3,#,2,#,1}]

That is, {3,1,4,#,2}, 3 is the root node of the two-fork tree why less? Think about it, the 3-node left child can be 1, or 2, then the left child for 1 of the situation is ignored, at this time Usenode is not equal to N, and then to the left child for the 2 node situation.

Correct code:
/** Definition of TreeNode: * Class TreeNode {* Public: * int val; * TreeNode *left, *right; * TreeNode (i NT val) {* This->val = val; * This->left = This->right = NULL; *} *}*/classSolution { Public:    /** * @paramn n:an integer * @return: A List of root*/Vector<treenode *> Buildt (intLdintRd) {Vector<treenode *>ans; if(LD = =Rd) {TreeNode*t =NewTreeNode (LD);            Ans.push_back (T); returnans; }        if(LD >Rd)            {Ans.push_back (NULL); returnans; }         for(intI=ld; i<=rd; ++i) {Vector<treenode *> ansleft = Buildt (ld, I-1); Vector<treenode *> ansright = Buildt (i+1, RD);  for(auto Lx:ansleft) for(Auto rx:ansright) {TreeNode*t =NewTreeNode (i); T->left =LX; T->right =Rx;                Ans.push_back (T); }        }        returnans; } Vector<treenode *> Generatetrees (intN) {//Write your code hereVector<treenode *> ans = Buildt (1, N); returnans; }};

Analysis: After determining the current node x, then x's left child node (or right child node) may have more than one, then put these possible nodes into the vector, then select the LX node from the left child collection, and select the RX node from the right child collection, Then LX and Rx determine a two-fork tree of a pattern.

N nodes, two forked trees of different shapes (number + spawn)

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.