PAT03-树1. List Leaves (25)

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)要从8组数据中找出树的根节点(root),我们可以采用设置标记位来解决,如果在所有节点的左右子树遍历中没有碰见过该节点,则说明该节点是跟节点。(2)关于树节点数据结构的定义。此题和普通树的定义略有不同,因为此树的给出是顺序给出的,并没有直接给出左右子节点的指针,只是给出了index,所以我们在定义结构时浪费点空间,将其左右子节点的索引和指针一并给出。(3)题目中最终要求按照从左到右从上到下的顺序将子节点打印出来,即采用层序遍历的方法。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

typedef struct TreeNode {
	int data;
	int tLeft;
	int tRight;
	struct TreeNode *left;
	struct TreeNode *right;
	struct TreeNode *next;
} tNode, *pNode;

typedef struct TreeQuene {
	pNode front;
	pNode rear;
}tQuene, *pQuene;

pQuene CreateQuene();
void AddQ(pQuene q, pNode node);
pNode DeleteQ(pQuene q);
//void OrderTraver(int root, pNode tPointArray[]);
int IsEmptyQuene(pQuene q);

int main()
{
	int lines, i;
	int left, right;
	char strleft, strright;
	pNode tPointerArray[MAXSIZE];
	pNode tPointer;

	scanf("%d", &lines);
	int flag[MAXSIZE] = {
		0
	};
	for (i = 0; i < lines; i++) {
		tPointer = (pNode)malloc(sizeof(tNode));
		getchar();
		scanf("%c %c", &strleft, &strright);
		if (strleft == ‘-‘) {
			left = -1;
		} else {
			left = (int)(strleft - ‘0‘);
			flag[left] = 1;
		}

		if (strright == ‘-‘) {
			right = -1;
		} else {
			right = (int)(strright - ‘0‘);
			flag[right] = 1;
		}

		tPointer->data = i;
		tPointer->tLeft = left;
		tPointer->tRight = right;
		tPointer->left = NULL;
		tPointer->right = NULL;
		tPointerArray[i] = tPointer;
	}

	int rootIndex;
	for (i = 0; i < lines; i++) {
		if (flag[i] != 1) {
			rootIndex = i;
		}
	}

	//create Tree
	for (i = 0; i < lines; i++) {
        if (tPointerArray[i]->tLeft != -1) {
            tPointerArray[i]->left = tPointerArray[(tPointerArray[i]->tLeft)];
        } else {
            tPointerArray[i]->left = NULL;
        }
        if (tPointerArray[i]->tRight != -1) {
            tPointerArray[i]->right = tPointerArray[(tPointerArray[i]->tRight)];
        } else {
            tPointerArray[i]->right = NULL;
        }

	}

	//root index
	pNode root = tPointerArray[rootIndex];

	//levelOrderTravelsal
	pQuene quene = CreateQuene();
	AddQ(quene, root);
	int flagg = 1;
	while (!IsEmptyQuene(quene)) {
		pNode node = DeleteQ(quene);
		if (!(node->left) && !(node->right)) {
			if (flagg) {
				printf("%d", node->data);
				flagg = 0;
			} else {
				printf(" %d", node->data);
			}
		}

		if (node->left) {
			AddQ(quene, node->left);
		}
		if (node->right) {
			AddQ(quene, node->right);
		}
	}

	return 0;
}

pQuene CreateQuene()
{
	pQuene q = (pQuene)malloc(sizeof(tQuene));
	q->front = NULL;
	q->rear = NULL;
	return q;
}

void AddQ(pQuene q, pNode node)
{
	if (!(q->rear)) {
		q->rear = node;
	} else {
		q->rear->next = node;
		q->rear = node;
	}

	if (!(q->front)) {
		q->front = node;
	}
}

pNode DeleteQ(pQuene q)
{
	pNode temp = q->front;
	if (temp) {
		q->front = q->front->next;
		return temp;
	} else {
		return NULL;
	}
}

int IsEmptyQuene(pQuene q)
{
	if (q->front == NULL) {
		return 1;
	} else {
		return 0;
	}
}
时间: 2024-11-02 02:21:42

PAT03-树1. List Leaves (25)的相关文章

03-树2. List Leaves (25) 二叉树的层序遍历

03-树2. List Leaves (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912 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

pat03-树2. List Leaves (25)

03-树2. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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.

PTA 树的同构(25 分)

7-1 树的同构(25 分) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树.而图2就不是同构的. 图1 图2 现给定两棵树,请你判断它们是否是同构的. 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N?1编号):随后N行,第i行对应编号第i个结点,给出

7-1 树的同构 (25 分)

题目 : 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树.而图2就不是同构的. 图一: 图二: 现给定两棵树,请你判断它们是否是同构的. 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N?1编号):随后N行,第i行对应编号第i个结点,给出该结点中存储的1

7-3 树的同构 (25 分)

题目地址: https://pintia.cn/problem-sets/15/problems/711 解决方法: 要判断树是否是同构,判定存储相同信息的节点的孩子(或父节点)是否一致即可; 推荐用结构体数组存储树  : 输入的节点  下标依次为 0 ==> n-1 根节点的判断:根据题目输入信息为 节点信息  左孩子 右孩子 :因此 孩子信息内没出现过的点即为根节点 样例分析 :以判断节点的父节点进行说明 根据两棵树对应的输入信息得出下表  LEFT A B C D E G G H Inde

List Leaves (25)

要按从上到下,从左到右的顺序查找叶子节点,可以采用广度优先搜索的办法 #include <iostream> #include <queue> using namespace std; typedef struct{ int loc; int left; int right; }unit; int n; unit* a; int father(int); void printLeaf(int); int main() { cin >> n; getchar(); a =

PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each inpu

51Nod 1272最大距离 (树状数组维护前缀最小值)

题目链接 最大距离 其实主流解法应该是单调栈--我用了树状数组. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i, a, b) for (int i(a); i <= (b); ++i) 6 7 const int N = 100010; 8 9 struct node{ 10 int x, y; 11 friend bool operator < (const node &a, c

poj 3841 Double Queue (AVL树入门)

1 /****************************************************************** 2 题目: Double Queue(poj 3481) 3 链接: http://poj.org/problem?id=3481 4 算法: avl树(入门) 5 *******************************************************************/ 6 #include<cstdio> 7 #inclu