03-2. List Leaves (PAT) - 二叉樹層序遍歷問題


 

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;
}

 





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM