题目:codevs 3143
思路:就递归解决遍历,用栈和队列也可以做到,但是有简单的何乐而不为;
代码如下:
#include<iostream>
#include<cstring>
using namespace std;
int a[17][2];
void prev_travel(int root)
{
if(root == 0) return;
cout << root << " ";
prev_travel(a[root][0]);
prev_travel(a[root][1]);
}
void mid_travel(int root)
{
if(root == 0) return;
mid_travel(a[root][0]);
cout << root << " ";
mid_travel(a[root][1]);
}
void order_travel(int root)
{
if(root == 0) return;
order_travel(a[root][0]);
order_travel(a[root][1]);
cout << root << " ";
}
int main()
{
int n;
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> a[i][0] >> a[i][1];
}
prev_travel(1);
cout << endl;
mid_travel(1);
cout << endl;
order_travel(1);
return 0;
}