• List Leaves


    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 (≤) 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

    AC代码

     1 #include<stdio.h>
     2 
     3 struct TreeNode{
     4   int Left;
     5   int Right;
     6 }T[10];
     7 
     8 int BTRead(struct TreeNode T[],int N);
     9 void FindLeavesofList(int R, struct TreeNode T[],int N);
    10 
    11 int main(){
    12   int R;
    13   int N;
    14   scanf("%d
    ",&N);
    15   R = BTRead(T,N);
    16   //printf("%d",R);
    17   FindLeavesofList(R,T,N);
    18   return 0;
    19 }
    20 
    21 int BTRead(struct TreeNode T[],int N){
    22   int Root = 0;
    23   //printf("ll%dll",Root);
    24   char c1,c2;
    25   if(!N){
    26     return -1;
    27   }
    28   int check[N];
    29   for(int i = 0; i < N; i++){
    30     check[i] = 0;
    31   }
    32   for(int i = 0; i < N; i++){
    33     scanf("%c %c
    ",&c1,&c2);
    34     if(c1 != '-'){
    35       T[i].Left = c1 - '0';
    36       check[T[i].Left] = 1;
    37     }
    38     else{
    39       T[i].Left = -1;
    40     }
    41     if(c2 != '-'){
    42       T[i].Right = c2 - '0';
    43       check[T[i].Right] = 1;
    44     }
    45     else{
    46       T[i].Right = -1;
    47     }
    48   }
    49   //printf("%d",check[0]);
    50   int i ;
    51   for(i = 0; i < N; i++){
    52     if(!check[i]){
    53       break;
    54     }
    55   }
    56   Root = i;
    57   return Root;
    58 }
    59 
    60 void FindLeavesofList(int R, struct TreeNode T[],int N){
    61   int Queue[1000];
    62   int pre = 0,rear = 0;
    63   int t;
    64   int sign = 0;
    65   for(int i = 0; i < N; i++){
    66     Queue[i] = -1;
    67   }
    68   if(R == -1){
    69     return;
    70   }else{
    71     Queue[rear] = R;
    72     rear++;
    73   }
    74   while(pre != rear){
    75     t = Queue[pre];
    76     Queue[pre] = -1;
    77     pre++;
    78     if(T[t].Left==-1&&T[t].Right==-1&&sign == 0){
    79       printf("%d",t);
    80       sign = 1;
    81   }else if(T[t].Left==-1&&T[t].Right==-1){
    82     printf(" %d",t);
    83   }
    84     if(T[t].Left != -1){
    85       Queue[rear] = T[t].Left;
    86       rear++;
    87     }
    88     if(T[t].Right != -1){
    89       Queue[rear] = T[t].Right;
    90       rear++;
    91     }
    92   }
    93 }



  • 相关阅读:
    js中apply的用法(转)
    JS匿名函数的理解
    winform 添加“设置文件”
    用VMware安装虚拟系统时出现Invalid system disk,Replace the disk and then press any key
    Easy Multiple Copy to Clipboard by ZeroClipboard
    SaltStack配置管理-状态间关系
    Docker容器之Nginx
    CentOS7.2升级默认yum安装的php版本
    升级PHP版本导致zabbix无法访问解决办法
    Piwik网站访问统计软件安装
  • 原文地址:https://www.cnblogs.com/jinjin-2018/p/8718586.html
Copyright © 2020-2023  润新知