03-树2 List Leaves (25 分)

代码链接:http://www.cnblogs.com/liangchao/p/4280402.html

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

// 创建静态链表储存二叉树 ? 

过程:

1:创建二叉树

2:寻找根结点

3:遍历二叉树

#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
		{                                  //c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.
			left = atoi(strleft.c_str());  //头文件 stdlib.h 字符串转换为整数 atoi 
			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);
		//cout << item << " ";
		if (tPointerArray[item]->tLeft == -1 && tPointerArray[item]->tRight == -1)
		{
			string Result;          // string which will contain the result
			//当存在未知数据大小的时候,可以使用 ostringstream 来代替 sprintf , 避免总是申请大量的缓冲区
			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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值