Binary search tree in the data structure to learn, but feel that they learn very water, recently in the Lintcode did two on the binary search tree, feel a comparison recorded, it is to enhance the memory!
1. Two fork Find tree I
Test instructions
Given n, ask how many different two-fork search trees are made up of 1...N nodes?
Examples:
Give n = 3, generate all 5 different patterns of two fork find tree:1 3 3 2 1 \ / / / \ 3 2 1 1 3 2/ / \ 2 1 2 3
This is very common in the two-fork tree in the data structure. This is an example of a typical Cattleya number.
(1). Cattleya number
Make h (0) =1,h (1) = 1, Cattleya number satisfies recursion: H (n) = h (0) *h (n-1) + H (1) *h (n-2) + ... + h (n-1) *h (0) (n>=2); The recursive formula is: H (n) = C (2n,n)/(n+1), n=0,1 , 2,3,...
(2). Why the binary search tree satisfies the Cattleya number?
Let's assume that.
When there is only one node, there must be only one case, so we assume that f (1) = 1;
When there are only two nodes, how to consider? We fixed a node, and the other node was either placed in the left subtree or placed in the right subtree, so there are two cases, f (2) = f (1) + f (1). where f (1), we understand that the first F (1) is actually f (1) * F (0) (where f (0) equals 1, why is f (0) equal to 1?). Because there is no node there is only one situation Ah! ), meaning that Zuozi has only one node, the right subtree has no nodes, and the second F (1) is represented by F (0) * F (1), which indicates that the left subtree has no nodes and the right subtree has only one node.
When there are only three or three nodes above, we assume that the case of N, f (n) is divided into the left subtree is 0 and the right subtree is n-1 (because one node is fixed, so only n-1 nodes), the left subtree is 1 and the right subtree is n-2, the left subtree is 2 and the right subtree is n-3 and so on ... Then we can exit the formula: F (n) = f (0) * F (n-1) + f (1) * F (n-2) + ... + f (n-2) * F (1) + f (n-1) * F (1).
(3). Dynamic Planning
Based on the recursive formula above, we can write dynamic programming code
Public int numtrees (int n) { intnewint[n + 1]; nums[0] = 1; for (int i = 1; I <= n; i++) { for (int j = 0; J < i; j + +) { /c19>+= Nums[j] * nums[i-1- j]; } } return Nums[n];
2. Two fork Find Tree II
Test instructions
Give n to generate all the different two-fork lookup trees that are made up of 1...N nodes
Examples:
Give n = 3, generate all 5 different patterns of two fork find tree:1 3 3 2 1 \ / / / \ 3 2 1 1 3 2/ / \ 2 1 2 3
To tell the truth, this problem really do not know how to write, the label is written deep search, but I do not know how to write it out, because previously only written deep search of the problem, have not done deep search of the generation type problem!
It is best that I find someone else's code on the Web, and then I understand it, and I don't know if I understand it correctly.
Take a look at the code first:
PublicList<treenode> Generatetrees (intN) {List<TreeNode> list =NewArraylist<>(); if(N < 0) { return NULL; } list= Createtree (1, N); returnlist; } PrivateList<treenode> Createtree (intStartintend) {List<TreeNode> list =NewArraylist<>(); if(Start >end) {List.add (NULL); returnlist; } for(inti = start; I <= end; i++) {List<TreeNode> left = Createtree (Start, i-1); List<TreeNode> right = Createtree (i + 1, end); for(intj = 0; J < Left.size (); J + +) { for(intk = 0; K < Right.size (); k++) {TreeNode node=NewTreeNode (i); Node.left=Left.get (j); Node.right=Right.get (k); List.add (node); } } } returnlist; }
I understand that: for example, n nodes, it is divided into n-1, the same as the Cattleya number above, and then we construct the current node Saozi right subtree, through the recursive structure. Because of the n-1, there is a loop to construct.
Algorithm-Different two-fork find tree I and II (Dynamic programming and deep search algorithms)