zoukankan      html  css  js  c++  java
  • 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 N1. 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 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <unistd.h>
      4 
      5 #define MaxTree 10
      6 #define Tree int
      7 #define Null -1
      8 
      9 struct TreeNode {
     10     Tree Left;
     11     Tree Right;
     12 } T1[MaxTree];
     13 
     14 #define QueueSize 100
     15 struct QNode {
     16     int Data[QueueSize];
     17     int rear;
     18     int front;
     19 };
     20 typedef struct QNode *Queue;
     21 
     22 int IsEmpty(Queue PtrQ)
     23 {
     24     return (PtrQ->front == PtrQ->rear);
     25 }
     26 
     27 void AddQ(Queue PtrQ, int item)
     28 {
     29     if((PtrQ->rear+1)%QueueSize == PtrQ->front) {
     30         printf("Queue full");
     31         return;
     32     }
     33     PtrQ->rear = (PtrQ->rear+1)%QueueSize;
     34     PtrQ->Data[PtrQ->rear] = item;
     35 }
     36 
     37 int DeleteQ(Queue PtrQ)
     38 {
     39     if(PtrQ->front == PtrQ->rear) {
     40         printf("Queue empty");
     41         return -1;
     42     } else {
     43         PtrQ->front = (PtrQ->front+1)%QueueSize;
     44         return PtrQ->Data[PtrQ->front];
     45     }
     46 }
     47 
     48 Tree BuildTree(struct TreeNode T[])
     49 {
     50     char cl, cr;
     51     int N, i, check[MaxTree];
     52     Tree Root = Null;
     53     // scanf("%d
    ", &N);
     54     scanf("%d
    ", &N);
     55     // printf("N=%d
    ", N);
     56     if(N) {
     57         for(i=0;i<N;i++)
     58             check[i] = 0;
     59         for(i=0;i<N;i++) {
     60             scanf("%c %c
    ", &cl, &cr);
     61             // printf("cl=%c, cr=%c
    ", cl, cr);
     62             if(cl!='-') {
     63                 T1[i].Left = cl - '0';
     64                 check[T1[i].Left] = 1;
     65             } else
     66                 T1[i].Left = Null; 
     67 
     68             if(cr!='-') {
     69                 T1[i].Right = cr - '0';
     70                 check[T1[i].Right] = 1;
     71             } else
     72                 T1[i].Right = Null;
     73         }
     74         for(i=0;i<N;i++)
     75             if(check[i] != 1) break;
     76         Root = i;
     77     }
     78     return Root;
     79 }
     80 
     81 void PrintLeaves(Tree R)    //层序遍历
     82 {
     83     Queue Q;
     84     Tree cur;
     85     int count = 0;
     86     if(R==Null) return;
     87     Q = (Queue)malloc(sizeof(struct QNode));
     88     Q->rear = -1;
     89     Q->front = -1;
     90     AddQ(Q, R);
     91     while(!IsEmpty(Q)) {
     92         cur = DeleteQ(Q);
     93         if((T1[cur].Left == Null) && (T1[cur].Right == Null)) {
     94             if(!count) {
     95                 printf("%d", cur);
     96                 count++;
     97             } else 
     98             printf(" %d", cur);
     99             continue;
    100         }
    101         if(T1[cur].Left!=Null) AddQ(Q, T1[cur].Left);
    102         if(T1[cur].Right!=Null) AddQ(Q, T1[cur].Right);
    103     }
    104 }
    105 
    106 int main()
    107 {
    108     Tree R;
    109     R = BuildTree(T1);
    110 
    111     PrintLeaves(R);
    112 
    113     return 0;
    114 }

    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    GLSL
    c++ 的垃圾收集(garbage collector
    ZZ 红黑树,并非想象中的那么复杂
    【转载】我心目中的android机器档次
    代码优化
    qqww
    solve Ax+By+C=0
    the c10k problem
    标 题: 腾讯面试题目(PHP程序员)
    zz 软件开发流程工具一览
  • 原文地址:https://www.cnblogs.com/ch122633/p/8744850.html
Copyright © 2011-2022 走看看