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; }
相关文章推荐
- 03-树2. List Leaves (25) 二叉树的层序遍历
- PAT 03-2. List Leaves (25)
- PAT 03-2. List Leaves (25)
- PAT 数据结构 03-树2. List Leaves (25)
- 03-2. List Leaves (25) 树的层次遍历
- 【解题报告】【浙大PAT】03-树1. List Leaves (25)
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
- Java实现带行号层序遍历二叉树的变种问题(使用二维数组保存)
- 03-树2. List Leaves (25) Python C
- 树2. List Leaves(树的遍历,队列的简单应用)
- 03-2. List Leaves (25)--c语言实现
- PAT 03-树1. List Leaves (25)(C)
- PAT 1020(由二叉树的中序和后序,求层序遍历) 1086(由二叉树的中序和先序求后序遍历)
- 03-2. List Leaves (25)
- 编程之美问题之二叉树层序遍历多种解法
- 03-树2. List Leaves(25)
- pat L2-006-已知二叉树的后序序列和中序序列求层序遍历结果
- Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
- 03-树2. List Leaves (25)
- 03-树2. List Leaves (25) Java