31,432 sequential traversal of a fork tree
time limit: 1 sspace limit: 32000 KBtitle level: Silver SolvingTitle Description
Description
To find a binary tree's pre-sequence traversal, middle sequence traversal and post-order traversal
Enter a description
Input Description
The first line is an integer n, which indicates the number of nodes in the tree.
The next n rows are 2 integer l and r per line. The two integers of line I, Li and Ri, represent the left son number and the right son number of the node numbered I.
Output description
Output Description
The output is three lines, namely, the pre-sequence traversal, the middle sequence traversal and the post-order traversal. The numbers are separated by a space.
Sample input
Sample Input
5
2 3
4 5
0 0
0 0
0 0
Sample output
Sample Output
1 2 4) 5 3
4 2 5) 1 3
4 5 2) 3 1
Data range and Tips
Data Size & Hint
N <= 16
Category labels
Tags Click here to expandRecursive search
#include <cstdio>inta[101][3],n;voidFrontintN) { if(!n)return ; printf ("%d", N); Front (a[n][1]); Front (a[n][2]); }voidMiddle (intN) { if(!n)return ; Middle (a[n][1]); printf ("%d", N); Middle (a[n][2]); }voidBehindintN) { if(!n)return ; Behind (a[n][1]); Behind (a[n][2]); printf ("%d", N); }intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) {a[i][0]=i; scanf ("%d%d", &a[i][1],&a[i][2]); } Front (a[1][0]); Puts (""); Middle (a[1][0]); Puts (""); Behind (a[1][0]); Puts (""); return 0;}
31,432 sequential traversal of a fork tree