18.2.26 codevs3143 二叉树的序遍历

题目描述 Description

求一棵二叉树的前序遍历,中序遍历和后序遍历

输入描述 Input Description

第一行一个整数n,表示这棵树的节点个数。

接下来n行每行2个整数L和R。第i行的两个整数Li和Ri代表编号为i的节点的左儿子编号和右儿子编号。

输出描述 Output Description

输出一共三行,分别为前序遍历,中序遍历和后序遍历。编号之间用空格隔开。

样例输入 Sample Input

5

2 3

4 5

0 0

0 0

0 0

样例输出 Sample Output

1 2 4 5 3

4 2 5 1 3

4 5 2 3 1

数据范围及提示 Data Size & Hint

n <= 16

  1 #include <iostream>
  2 #include <string.h>
  3 #include <math.h>
  4 
  5 using namespace std;
  6 int tree[20][3],tree1[20][3]={0},tree2[20][3]={0},tree3[20][3]={0};
  7 int visited1[20]={0},visited2[20]={0},visited3[20]={0};
  8 int flag2=0,flag3=0;
  9 
 10 void forw(int n)//前序遍历
 11 {
 12     if(!visited1[n])
 13     {
 14         if(n!=1)
 15             cout<<" ";
 16         cout<<n;
 17         visited1[n]=1;
 18     }
 19     if(tree1[n][1]+tree1[n][2]==0)
 20         return;
 21     else if(tree1[n][1]==0)
 22     {
 23         int tmp=n;
 24         n=tree1[n][2];
 25         forw(n);
 26         tree1[tmp][2]=0;
 27         n=tmp;
 28         return;
 29     }
 30     else
 31     {
 32         int tmp=n;
 33         n=tree1[n][1];
 34         forw(n);
 35         tree1[tmp][1]=0;
 36         n=tmp;
 37         forw(n);
 38     }
 39 }
 40 
 41 void mid(int n)//中序遍历
 42 {
 43     if(visited2[tree2[n][1]])
 44     {
 45         if(flag2)
 46            cout<<" ";
 47         flag2=1;
 48         cout<<n;
 49         visited2[n]=1;
 50     }
 51     if(tree2[n][1]+tree2[n][2]==0)
 52     {
 53         if(flag2)
 54            cout<<" ";
 55         flag2=1;
 56         cout<<n;
 57         visited2[n]=1;
 58         return;
 59     }
 60     else if(visited2[tree2[n][1]]&&visited2[tree2[n][2]])
 61         return;
 62     else if(tree2[n][1]==0)
 63     {
 64         if(flag2)
 65            cout<<" ";
 66         flag2=1;
 67         cout<<n;
 68         visited2[n]=1;
 69         int tmp=n;
 70         n=tree2[n][2];
 71         mid(n);
 72         tree2[tmp][2]=0;
 73         n=tmp;
 74         return;
 75     }
 76     else
 77     {
 78         int tmp=n;
 79         n=tree2[n][1];
 80         mid(n);
 81         tree2[tmp][1]=0;
 82         n=tmp;
 83         mid(n);
 84     }
 85 }
 86 
 87 void backw(int n)//后序遍历
 88 {
 89     if((visited3[tree3[n][1]]||!tree3[n][1])&&(visited3[tree3[n][2]]||!tree3[n][2]))
 90     {
 91         if(flag3)
 92            cout<<" ";
 93         flag3=1;
 94         cout<<n;
 95         visited3[n]=1;
 96         return;
 97     }
 98     if(!tree3[n][1]||visited3[tree3[n][1]])
 99     {
100         backw(tree3[n][2]);
101         backw(n);
102     }
103     else
104     {
105         backw(tree3[n][1]);
106         backw(n);
107     }
108 }
109 
110 int main()
111 {
112     int n;
113     cin>>n;
114     for(int i=1;i<=n;i++)
115     {
116         cin>>tree1[i][1];
117         tree2[i][1]=tree1[i][1];
118         tree3[i][1]=tree2[i][1];
119         cin>>tree1[i][2];
120         tree2[i][2]=tree1[i][2];
121         tree3[i][2]=tree2[i][2];
122     }
123     forw(1);
124     cout<<endl;
125     mid(1);
126     cout<<endl;
127     backw(1);
128     cout<<endl;
129     return 0;
130 }
View Code

前中后序遍历实质上是把根部放在左右儿子前面,中间,后面输出。

注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
原文地址:https://www.cnblogs.com/yalphait/p/8471708.html