完全二叉树的前序遍历,但用不着真的构建一个完全二叉树,可以使用数组来进行模拟。具体就是构建一个1000的数组,每个位置包含有节点内容、左子树节点、右子树节点即可,然后找出根节点,再使用递归进行前序遍历。
Run Time: 0sec
Run Memory: 324KB
Code length: 923Bytes
SubmitTime: 2012-01-04 17:02:20
// Problem#: 1156
// Submission#: 1172813
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstring>
using namespace std;
struct Node {
char content;
int left, right;
};
Node node[ 1001 ];
bool exist[ 1001 ];
bool root[ 1001 ];
void preorder( int n ) {
if ( n != 0 ) {
cout << node[ n ].content;
preorder( node[ n ].left );
preorder( node[ n ].right );
}
}
int main()
{
int n, i;
while ( cin >> n ) {
memset( exist, false, sizeof( exist ) );
memset( root, true, sizeof( root ) );
while ( n-- ) {
cin >> i;
cin >> node[ i ].content >> node[ i ].left >> node[ i ].right;
exist[ i ] = true;
root[ node[ i ].left ] = false;
root[ node[ i ].right ] = false;
}
for ( i = 1; i <= 1000; i++ ) {
if ( exist[ i ] && root[ i ] )
break;
}
preorder( i );
cout << endl;
}
return 0;
}