当前位置:   article > 正文

03-树2 List Leaves(浙大数据结构PTA习题)

03-树2 List Leaves(浙大数据结构PTA习题)

03-树2 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:

  1. 8
  2. 1 -
  3. - -
  4. 0 -
  5. 2 7
  6. - -
  7. - -
  8. 5 -
  9. 4 6

Sample Output:

4 1 5

代码长度限制:16 KB        时间限制:400 ms        内存限制:64 MB

题目解析:

题目大意:给定一系列树的结点,按照从上到下,从左到右的顺序输出这棵树的叶子结点

关键点1:找到树的根结点

        

关键点2:根据树的根结点以及其它一系列结点构建树

         采用递归的思路,如果根结点的左子树不为空,则递归构建左子树,否则为NULL;如果根结点的右子树不为空,则递归构建右子树,否则为NULL;

关键点3:按照从上到下,从左到右的顺序输出这棵树的叶子结点

        本质是树的层序遍历问题,借助队列实现;

参考代码:

  1. # include<stdio.h>
  2. # include<stdbool.h>
  3. # include<stdlib.h>
  4. # define MAXNODE 10
  5. typedef char ElementType;
  6. typedef struct TreeNode* Tree;
  7. struct TreeNode{
  8. ElementType info,left,right;
  9. Tree Left;
  10. Tree Right;
  11. };
  12. Tree Create();
  13. Tree InitialTree(Tree Array[],int N);
  14. Tree CreateTree(Tree Array[],Tree Root);
  15. void InOrderPrint(Tree tree);
  16. int main(){
  17. // 根据输入创建树
  18. Tree tree = Create();
  19. // 对这棵树进行层序遍历,并输出叶子结点
  20. InOrderPrint(tree);
  21. return 0;
  22. }
  23. // 对一棵树进行层序遍历,并输出叶子结点
  24. void InOrderPrint(Tree tree){
  25. // 创建一个结点指针队列
  26. Tree Queue[MAXNODE];
  27. int Rear=-1, Head = -1;
  28. // 压入根结点
  29. Queue[++Rear] = tree;
  30. // 用于格式化输出的统计
  31. int count = 0;
  32. while(Rear!=Head){
  33. // 从队列出队一个结点,并判读是否是叶节点
  34. Tree tmp = Queue[++Head];
  35. if(tmp->Left == NULL && tmp->Right==NULL){
  36. if(count==0){
  37. printf("%d",tmp->info-'0');
  38. count++;
  39. }
  40. else printf(" %d",tmp->info-'0');
  41. }
  42. // 分别压入其左右结点(若不为空)
  43. if(tmp->Left)Queue[++Rear] = tmp->Left;
  44. if(tmp->Right)Queue[++Rear] = tmp->Right;
  45. }
  46. return;
  47. }
  48. // 根据输入创建一棵树
  49. Tree Create(){
  50. int N;
  51. scanf("%d",&N);
  52. getchar();
  53. if(N==0)return NULL;
  54. Tree Array[N];
  55. // 将信息存入指针数组中并获得树的根结点
  56. Tree Root = InitialTree(Array,N);
  57. // 通过树的根结点来建立树
  58. Tree tree = CreateTree(Array,Root);
  59. return tree;
  60. }
  61. // 将结点信息存入指针数组中,并返回树的根结点
  62. Tree InitialTree(Tree Array[],int N){
  63. // Check数组用来标记结点是否作为了子节点
  64. int i,Check[N];
  65. for(i=0;i<N;i++)Check[i] = 1;
  66. // 读入结点信息,并判读每个结点是否作为了子节点
  67. for(i=0;i<N;i++){
  68. Tree node = (Tree)malloc(sizeof(struct TreeNode));
  69. node->info = '0'+i;
  70. node->left = getchar();
  71. if(node->left!='-')Check[node->left-'0'] = 0;
  72. getchar();
  73. node->right = getchar();
  74. if(node->right!='-')Check[node->right-'0'] = 0;
  75. getchar();
  76. node->Left = node->Right = NULL;
  77. Array[i] = node;
  78. }
  79. for(i=0;i<N;i++){
  80. if(Check[i]==1)break;
  81. }
  82. return Array[i];
  83. }
  84. // 根据指针数组及根结点递归构建一棵树
  85. Tree CreateTree(Tree Array[],Tree Root){
  86. int i,count;
  87. // 递归构建左子树
  88. if(Root->left == '-'){
  89. Root->Left = NULL;
  90. }else{
  91. Root->Left = CreateTree(Array,Array[Root->left-'0']);
  92. }
  93. // 递归构建右子树
  94. if(Root->right == '-'){
  95. Root->Right = NULL;
  96. }else{
  97. Root->Right = CreateTree(Array,Array[Root->right-'0']);
  98. }
  99. return Root;
  100. }

运行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/654304
推荐阅读
相关标签
  

闽ICP备14008679号