首页 > 其他 > 详细

[ CodeVS冲杯之路 ] P3143

时间:2016-09-10 20:41:40      阅读:151      评论:0      收藏:0      [点我收藏+]

   不充钱,你怎么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("\n");
39     zhong(1);
40     printf("\n");
41     hou(1);
42     printf("\n");
43     return 0;
44 }

 

[ CodeVS冲杯之路 ] P3143

原文:http://www.cnblogs.com/hadilo/p/5860102.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!