二叉树的序遍历

来源:互联网 发布:天策成男捏脸数据 编辑:程序博客网 时间:2024/06/03 15:03

http://wikioi.com/problem/3143/

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<bitset>#include<iomanip>using namespace std;int a[ 20 ][ 3 ] ;void work1(int x)/////////////////////////////////////////////先序遍历{printf("%d ",x);if (a[x][1]!=0) work1(a[x][1]);if (a[x][2]!=0) work1(a[x][2]);}void work2(int x) /////////////////////////////////////////////中序遍历 {if (a[x][1]!=0)work2(a[x][1]);printf("%d ",x);if (a[x][2]!=0) work2(a[x][2]);}void work3(int x)/////////////////////////////////////////////后续遍历{if (a[x][1]!=0) work3(a[x][1]);if (a[x][2]!=0) work3(a[x][2]);printf("%d ",x);}int main(){int n ;while( scanf( "%d" , &n ) != EOF ){for( int i = 1 ; i <= n ; ++i ) scanf( "%d%d" ,&a[ i ][ 1 ] , &a[ i ][ 2 ] ) ; work1( 1 ) ;cout << endl ;work2( 1 ) ;cout << endl ;work3( 1 ) ; cout << endl ;}return 0 ;}


原创粉丝点击