您的位置:首页 > 其它

03-2. List Leaves (PAT) - 二叉树层序遍历问题

2015-02-08 20:14 525 查看

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.Input Specification:Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.Output Specification:For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:
4 1 5

题意:给出每个结点的子结点,要求按照从左至右从上至下的顺序输出每个叶子结点。
比如,在示例输入中结点0的子结点为1和空,结点2的子结点为空和空,结点3的子结点为0和空·····(输入的结点从上到下是按0~n的顺序给出的,题目没有说的很清楚)。
另外,输入中没有给出的那个点即为根结点。

解决思路:
  根据输入建立二叉树
  找出根结点
  层序遍历该二叉树(利用队列,队列中存储指向二叉树结点的指针)
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define MaxNum 10

typedef struct TreeNode{
int Data;
int tLeft;
int tRight;
}*pNode, tNode;

typedef struct TreeQueue{
int QueueData[MaxNum];
int rear;
int front;
}tQueue, *pQueue;

pQueue CreateQueue();
void AddQ( pQueue q, int item );
int DeleteQ( pQueue q );
void OrderTraver( int root, pNode tPointerArray[] );
bool IsEmptyQueue( pQueue q );

int main()
{
int lines, i;
int left, right;
string strleft, strright;
pNode tPointerArray[MaxNum];
pNode tPointer;
bool flag[MaxNum] = {false};
cin >> lines;
for ( i = 0; i < lines; i++ )
{
tPointer = ( pNode )malloc( sizeof( tNode ) );
cin >> strleft >> strright;
if ( strleft == "-" )
{
left = -1;
}
else
{
left = atoi(strleft.c_str());
flag[left] = true;
}
if ( strright == "-" )
{
right = -1;
}
else
{
right = atoi(strright.c_str());
flag[right] = true;
}
tPointer->Data = i;
tPointer->tLeft = left;
tPointer->tRight = right;
tPointerArray[i] = tPointer;
}
int root;
for( i = 0; i < lines; i++ )
{
if ( flag[i] != true )
{
root = i;
}
}
OrderTraver( root, tPointerArray );
return 0;
}

pQueue CreateQueue()
{
pQueue q = (pQueue)malloc( sizeof( tQueue ) );
return q;
}

void AddQ( pQueue q, int item )
{
if ( ( q->rear + 1) % MaxNum == q->front  )
{
cout << "queue full" << endl;
return;
}
q->rear = ( q->rear + 1 ) % MaxNum;
q->QueueData[q->rear] = item;
}

int DeleteQ( pQueue q )
{
if ( q->front == q->rear )
{
cout << "Queue Empty";
} else {
q->front = ( q->front + 1 ) % MaxNum;
return q->QueueData[ q->front ];
}
}

bool IsEmptyQueue( pQueue q )
{
if ( q->front == q->rear )
{
return true;
}
else
{
return false;
}
}

void OrderTraver( int root, pNode tPointerArray[] )
{
pQueue q;
if ( root == -1 )    return;
int bt = root;
int item;
string resultstr = "";
q = CreateQueue();
AddQ( q,  tPointerArray[bt]->Data);
while( !IsEmptyQueue( q ) )
{
item = DeleteQ( q );
if ( tPointerArray[item]->tLeft == -1 && tPointerArray[item]->tRight == -1 )
{
string Result;          // string which will contain the result
ostringstream convert;   // stream used for the conversion
convert << item;      // insert the textual representation of 'Number' in the characters in the stream
resultstr +=  convert.str() + ' ';
}
if ( tPointerArray[item]->tLeft != -1 )
{
AddQ( q,  tPointerArray[item]->tLeft);
}
if ( tPointerArray[item]->tRight != -1 )
{
AddQ( q,  tPointerArray[item]->tRight);
}
}
resultstr.pop_back();
cout << resultstr;
return;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: