List Leaves【数据结构测试3.2】

题目:

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


题目说明:(参考:http://blog.csdn.net/conjimmy/article/details/42118037)

此题输入的不是完全二叉树的那种顺序编码,而是类似于链表一样,输入节点带有子节点信息,而树的结构和根结点需要自己确定。

代码注释比较多了:

// ListLeaves.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;

#define MAXSIZE 10
typedef struct TNode *BinTree;
typedef struct TNode
{
	BinTree left;
	BinTree right;
	int isRoot;
	int data;
}TNode;

typedef struct Queue
{
	TNode* node[MAXSIZE];
	int rear;	
	int front;
}Queue;

bool isEmpty(Queue *queue)
{
	if(queue->front == queue->rear)
		return true;
	return false;
}

Queue* initQueue(Queue* queue)//头尾相等-初始化节点
{
	queue->front = queue->rear = 0;
	return queue;
}

void push(Queue* queue,TNode* node)
{
	queue->node[queue->rear] = node;
	queue->rear++;//增加后尾位置后移
}

TNode* pop(Queue *queue)
{
	TNode* temp = queue->node[queue->front];
	queue->front++;//删除后头位置后移
	return temp;//返回弹出节点的指针
}
TNode* list[MAXSIZE];
TNode* inProc()//读入节点,处理,返回根结点
{
	int n;//节点个数
	cin>>n;
	if(n == -1)	return NULL;

	char ileft[2],iright[2];//左右子节点
	TNode* bt = NULL;//开始建立树
	for(int i = 0; i < n; i++)//读入n组数据,并将'-'以null存储到树中,其余按int存入树中
	{
		cin>>ileft>>iright;
		bt = (TNode*)malloc(sizeof(TNode));
		
		//处理左
		if(ileft[0] == '-'){
			bt->left = NULL;
		}
		else{
			bt->left = (TNode*)malloc(sizeof(TNode));//先开左子结点空间,再给其存数据
			bt->left->data = atoi(ileft);// 转换成int
		}

		//处理右
		if(iright[0] == '-'){
			bt->right = NULL;
		}
		else{
			bt->right =  (TNode*)malloc(sizeof(TNode));
			bt->right->data = atoi(iright);
		}
		list[i] = bt;//按输入顺序存储的节点
	}

	for(int i = 0; i < n; i++)//标记非根结点
	{
		TNode* p = NULL;//利用指针p给原来list中的数据建立连接
		p = list[i];
		if(p->left != NULL)
		{
			int tmp = p->left->data;
			p->left = list[tmp];//建立list中存储数据与指向的数据的连接例如(list[3]存储的是2和7,就建立list[3]->left和list[2]、eftlist[7]之间联系)
			p->left->data = tmp;
			p->left->isRoot = 1;
			//cout<<"bt is:"<<p->left->data<<endl;
			//list[list[i]->left->data]->isRoot = 1;//1代表不是root
		}
		if(p->right != NULL)
		{
			int tmp = p->right->data;
			p->right = list[tmp];//建立list中存储数据与指向的数据的连接例如(list[3]存储的是2和7,就建立list[3]->left和list[2]、eftlist[7]之间联系)
			p->right->data = tmp;
			p->right->isRoot = 1;
			//cout<<"bt is:"<<p->right->data<<endl;
			//list[list[i]->right->data]->isRoot = 1;
		}
	}
	//寻找根结点
	TNode* root = NULL;
	for(int i = 0; i < n; i++)
	{
		root = list[i];
		if(root->isRoot != 1)
			break;
	}
	return root;//返回根结点
}


int main()
{
	Queue queue;
	Queue *q = &queue;
	q = initQueue(q);
	TNode * root = inProc();
	push(q,root);
	int isFirst = 1;//标记是否是第一个,不是第一个要先输出空格
	while (!isEmpty(q))
	{//层序遍历的方法:根结点入队,循环(抛出元素并访问,访问(或入队)左儿子,访问(或入队)右儿子)
		TNode * tmp=pop(q);//抛出
		if((tmp->right==NULL)&&(tmp->left==NULL))//访问
		{
			if(isFirst)
			{
				cout<<tmp->data;
				isFirst = 0;
			}
			else
			{
				cout<<" "<<tmp->data;
			}
		}

		if(tmp->left)//左子结点入队
			push(q,tmp->left);
		if(tmp->right)//右子节点入队
			push(q,tmp->right);
	}


	return 0;
}


你可能感兴趣的:(列出叶节点)