Topic Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_ problem&problem=653
Type of topic: data structure, two fork tree
Sample input:
3
x1 X2 X3
00000111
4
010 000
x3 3 x1 x2 00010011
4
010
0
Sample output:
S-tree #1:
0011
s-tree #2:
0011
The main effect of the topic:
Give a sequence set Vvi {x1, x2, x3, ..., xn}, Vvi is not 0 is 1 then there is an n-level tree, each layer of the same layer is the same number, which is taken from the VVI, the input will give each layer is the vvi of which one
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45527.htm
The last layer is the leaf node, which is a string of given numbers.
Starting with the node, if the knot is 0, go to the left son direction, if it is 1, go to the right son direction. Finally falls on the last layer of the leaf node, outputting this number
Ideas for solving problems:
This question should be regarded as a very water topic in this binary tree. No achievement, if it is 1 on the current node *2+1, if it is 0 on the multiply. Finally get a number. This number subtracts the sum of the nodes in the front layer and then outputs the corresponding results.
Look at the code specifically.
#include <iostream> #include <cstdio> #include <vector> using namespace std;
int n,m;
Char termi[200];
Char vva[200];
vector<int>vtorder;
inline void Solve () {char temp[10];
int Val;
Vtorder.clear ();
for (int i=0; i<n; ++i) {scanf ("%s", temp);
SSCANF (temp+1, "%d", &val);
Vtorder.push_back (Val);
} scanf ("%s", Termi);
scanf ("%d", &m);
while (m--) {scanf ("%s", vva+1);
int Pos=1;
for (int i=0; i<vtorder.size (); ++i) {if (vva[vtorder[i]]== ' 0 ') pos = pos*2;
else pos = pos*2+1;
printf ("%c", termi[pos-(1<<n));
printf ("\ n");
int main () {freopen ("Input.txt", "R", stdin);
int Cas=1;
while (scanf ("%d", &n) && N) {printf ("S-tree #%d:\n", cas++);
Solve (); printf ("\ n");
return 0; }