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