[ CodeVS冲杯之路 ] P3143

   不充钱,你怎么AC?

       题目:http://codevs.cn/problem/3143/

       大水题一道,只要会遍历,这里讲一下思路

       先序遍历:先输出,然后左儿子,最后右儿子

       中序遍历:先左儿子,再输出,最后右儿子

       后序遍历:先左儿子,然后右儿子,最后输出

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 #define N 20
 8 using namespace std;
 9 
10 int l[N],r[N],n;
11 void qian(int x)
12 {
13     if (x==0) return;
14     printf("%d ",x);
15     qian(l[x]);
16     qian(r[x]);
17 }
18 void zhong(int x)
19 {
20     if (x==0) return;
21     zhong(l[x]);
22     printf("%d ",x);
23     zhong(r[x]);
24 }
25 void hou(int x)
26 {
27     if (x==0) return;
28     hou(l[x]);
29     hou(r[x]);
30     printf("%d ",x);
31 }
32 int main()
33 {
34     int i;
35     scanf("%d",&n);
36     for (i=1;i<=n;i++) scanf("%d%d",&l[i],&r[i]);
37     qian(1);
38     printf("
");
39     zhong(1);
40     printf("
");
41     hou(1);
42     printf("
");
43     return 0;
44 }
原文地址:https://www.cnblogs.com/hadilo/p/5860102.html