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

    给定一棵树,您应该按照自上而下和从左到右的顺序列出所有叶子。

     1 #include <stdio.h>
     2 #include <queue>
     3 #include <algorithm>
     4 #define MaxTree 11
     5 #define Null -1 
     6 using namespace std;
     7 
     8 int check[MaxTree];
     9 int P[MaxTree];
    10 int num=0;
    11 queue<int> qu;
    12 
    13 struct TreeNode
    14 {
    15     int Left;
    16     int Right; 
    17 }T[MaxTree];
    18 
    19 int BuildTree(struct TreeNode T[])
    20 {
    21     int N,i;
    22     char le,ri;
    23     int Root;
    24     scanf("%d
    ",&N);
    25     if(N)
    26     {
    27         for(i=0;i<N;i++)
    28         {
    29             check[i]=0;
    30         }
    31         for(i=0;i<N;i++)
    32         {
    33             scanf("%c %c
    ",&le,&ri);
    34             if(le!='-')
    35             {
    36                 T[i].Left=le-'0';
    37                 check[T[i].Left]=1;
    38             }
    39             else T[i].Left=Null;
    40             if(ri!='-')
    41             {
    42                 T[i].Right=ri-'0';
    43                 check[T[i].Right]=1;
    44             }
    45             else T[i].Right=Null;
    46         }
    47         for(i=0;i<N;i++)
    48         {
    49             if(!check[i]) break;
    50         }
    51         Root = i;
    52     }
    53     else 
    54     {
    55         Root=Null;
    56     }
    57     return Root;
    58 }
    59 
    60 void search(int Tree)
    61 {
    62     if(Tree!=Null)
    63     {
    64         qu.push(Tree);
    65         while(!qu.empty())
    66         {
    67             int k;
    68             k=qu.front();
    69             if(T[k].Left==Null&&T[k].Right==Null)
    70                 P[num++]=k;
    71             qu.pop();;
    72             if(T[k].Left!=Null)
    73                 qu.push(T[k].Left);
    74             if(T[k].Right!=Null)
    75                 qu.push(T[k].Right);
    76         }
    77     }
    78     else return;
    79 }
    80 
    81 int main()
    82 {
    83     int Tree;
    84     Tree=BuildTree(T);
    85     search(Tree);
    86     int i;
    87     for(i=0;i<num;i++)
    88     {
    89         if(i==0)
    90             printf("%d",P[i]);
    91         else printf(" %d",P[i]);
    92     }
    93     return 0;
    94 }

  • 相关阅读:
    第一篇阅读笔记
    课程信息管理系统
    HDU1124求n的阶乘后0的个数
    分解质因数算法
    牛客小白月赛23 B阶乘(质因数分解)
    JAVAWEB将图片铺满整个页面的方法
    Codeforces Global Round 7
    POJ--1703并查集(区分两个集合)
    POJ--1611经典并查集
    DFS,BFS回顾(各种题)(肺炎疫情中,祝平安)
  • 原文地址:https://www.cnblogs.com/jiamian/p/10714338.html
Copyright © 2011-2022 走看看