Topic Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_ problem&problem=489
Type of topic: data structure, two fork tree
The main effect of the topic:
For two sets of numbers, all on the same tree of two forks, the first group is in order to traverse (inorder) sequentially output, the second group is the Sequential traversal (postorder) output, based on the two sets of data to build the original two-fork tree, and then calculate from the root node to each leaf node in the path of the sum of numbers, The smallest sum of output.
Sample input:
3 2 1 4 5 7 6 3 1 2 5 6 7 4 7 8 one 3
5
255
8
Sample output:
1
3
255
Analysis:
This problem is the use of two-tree reconstruction, and traversal.
Traversal of binary tree: first-order traversal, middle-order traversal, sequential traversal
As long as there is a sequence of middle sequence plus another sequence can only reconstruct the original two-fork tree.
After the two-fork tree reconstruction, as long as the binary tree search, get the sum of each path, and then find the smallest and can.
By the middle sequence traversal and the first sequence traversal, the following step traversal to build the method:
From the middle sequence and the sequential traversal sequence to build, returns the root node pointer
node * inpostcreatetree (int *mid,int *post,int len) {
if (len = = 0) return
null;
int i=len-1;
while (Post[len-1]!= mid[i])--
I.
Node *h=newnode ();
h->data=post[len-1];
H->left=inpostcreatetree (mid,post,i);
H->right=inpostcreatetree (mid+i+1,post+i,len-i-1);
return h;
}
A contribution from the sequence of pre and middle sequence traversal, returning the pointer to the root node
//This URL address: http://www.bianceng.cn/Programming/sjjg/201410/45532.htm
Node * Preincreatetree (int *mid,int *pre,int len) //n identification s2 length
{
if (len==0) return
NULL;
int i = 0;
while (*mid!= pre[i])
++i;
Node *h=newnode ();
H->data= *mid;
H->left = Preincreatetree (mid+1, Pre, i);
H->right = Preincreatetree (mid+i+1, pre+i+1, len-i-1);
return h;
}
AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace St
D
const int MAXN = 10005;
int INORDER[MAXN], postorder[maxn], nindex;
Class node{Public:int data;
Node *left;
Node *right;
};
int nodeindex;
Node NODE[MAXN];
vector<int>result;
vector<node*>presult;
BOOL Flag;
int ans;
Inline node* NewNode () {node[nodeindex].left = NULL;
Node[nodeindex].right = NULL;
Return &node[nodeIndex++];
} inline void input () {nindex=1;
while (GetChar ()!= ' \ n ') scanf ("%d", &inorder[nindex++));
Enter the second line, then iterate through for (int i=0; i<nindex; ++i) scanf ("%d", &postorder[i)); ///From the sequence of sequences and sequential traversal, returns the root node pointer node * inpostcreatetree (int *mid,int *post,int len) {if (len = = 0) r
Eturn NULL;
int i=len-1;
while (Post[len-1]!= mid[i])--I. Node *h=newnode ();
h->data=post[len-1];
H->left=inpostcreatetree (Mid,post,i);
H->right=inpostcreatetree (mid+i+1,post+i,len-i-1);
return h; } void Dfs (Node *root, int n) {if (!root->left &&!root->right) {result.push_back (N+ro
Ot->data);
Presult.push_back (root);
return;
} if (Root->left) DFS (Root->left, n+root->data);
if (root->right) DFS (root->right, n+root->data);
int main () {freopen ("Input.txt", "R", stdin);
while (~SCANF ("%d", &inorder[0])) {input ();
Nodeindex = 0;
Node *root = Inpostcreatetree (inorder, Postorder, nindex);
Result.clear ();
Presult.clear ();
DFS (root, 0);
int minpos = 0;
for (int i=1; i<result.size (); ++i) if (result[i) < Result[minpos]) minpos=i;
printf ("%d\n", presult[minpos]->data);
return 0; }