zoukankan      html  css  js  c++  java
  • 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 }



  • 相关阅读:
    你若不努力,整个世界将与你无关
    【规范】yii2 resetful 授权验证
    DFT到FFT的理解
    【统计学】6.统计量及其抽样分布
    【统计学】5.概率与概率分布
    【统计学】4.数据的概括性度量
    【统计学】3.数据的图表展示
    【统计学】2.数据的搜集
    【统计学】1.导论
    小程序API(1.19)利用API函数设置标签栏的方法
  • 原文地址:https://www.cnblogs.com/jinjin-2018/p/8718586.html
Copyright © 2011-2022 走看看