Topic 1521: Two mirror image of a fork tree
-
time limit:1 seconds
Memory limit:128 MB
Special question: No
submitted:2061
Resolution:560
-
Title Description:
-
Enter a binary tree to output its image.
-
Input:
-
The input may contain multiple test samples, and the input ends with EOF.
For each test case, the first behavior of the input is an integer n (0<=n<=1000,n represents the number of two fork tree nodes that will be entered (the node is numbered starting from 1). The next line has n numbers that represent the values of the elements of the I binary tree node. Next there are n lines, one letter ci per line.
Ci= ' d ' indicates that the first node has two children, followed by the left child number and the right child number.
Ci= ' l ' indicates that the first node has a left child, followed by the number of the left child.
Ci= ' R ' indicates that the first node has a right child, followed by the number of the right child.
Ci= ' Z ' indicates that the first node has no child.
-
Output:
-
corresponding to each test case,
Outputs the element value of its child node according to the preamble.
Null if the output is empty.
-
Sample input:
-
6 5 7 9 11d 2 3d 4 5d 6 7zzzz
-
Sample output:
-
8 10 11 9 6 7 5
Note: For single character input, do not use scanf!!
1#include <cstdio>2#include <cstring>3#include <string>4#include <queue>5#include <stack>6#include <iostream>7 using namespacestd;8 structnode{9 intl,r,v;Ten }; OneNode nodes[1005]; A voidReserveintnum) { - if(NODES[NUM].L) - Reserve (NODES[NUM].L); the if(NODES[NUM].R) - Reserve (NODES[NUM].R); - intnn=NODES[NUM].L; -Nodes[num].l=NODES[NUM].R; +Nodes[num].r=nn; - } + voidPrefind (intnum) { A if(!num) at return; - if(num>1) -printf" "); -printf"%d", NODES[NUM].V); - Prefind (NODES[NUM].L); - Prefind (NODES[NUM].R); in } - intMain () { to //freopen ("D:\\input.txt", "R", stdin); + intN; - while(SCANF ("%d", &n)! =EOF) { the inti; * for(i=1; i<=n;i++){ $scanf"%d",&nodes[i].v);Panax Notoginseng } - //Achievements the for(i=1; i<=n;i++){ + CharC; Acin>>c;//for entering a single character, do not use SCANF!!! the //scanf ("%c", &c); + if(c=='D'){ -scanf"%d%d",&nodes[i].l,&NODES[I].R); $}Else{ $ if(c=='L'){ -scanf"%d",&nodes[i].l); -Nodes[i].r=0; the } - Else{Wuyi if(c=='R'){ thescanf"%d",&NODES[I].R); -Nodes[i].l=0; Wu } - Else{//c== ' z ' AboutNodes[i].l=0; $Nodes[i].r=0; - } - } - } A } + if(!N) { theprintf"null\n"); - } $ Else{ theReserve1);//Mirror thePrefind (1);//Pre-sequence traversal the } the } - return 0; in}
Nine degrees OJ topic 1521: Two mirror image of the fork Tree