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:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5

這是一個簡單的二叉樹,根據題目給出的結點建立一顆樹,然後按層次遍歷,把葉節點按順序輸出。

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <set>
 4 #include <queue>
 5 #include <cstring>
 6 #include <string>
 7 using namespace std;
 8 
 9 struct node //結點結構體 
10 {
11        int leftchild;  //左兒子 
12        int rightchild;  //右兒子 
13        node ()     //初始化 
14        {
15             leftchild=-1;
16             rightchild=-1;
17        }
18 };
19 node n[20];
20 bool r[20];  //記錄是否是根結點 
21 queue<int> q;   //有佇列遍歷樹 
22 int t=0;
23 
24 void traversal(int a)   //根據根節點a遍歷樹並且把也結點輸出 
25 {
26      q.push(a);
27      while (!q.empty())
28      {
29            
30            int j=q.front();
31            //cout <<"q.pop() "<<j<<endl;
32            q.pop();
33            if (n[j].leftchild==-1&&n[j].rightchild==-1)//沒有左兒子和有兒子則為葉子 
34            {
35                  if (t==0)//第一個葉節點直接輸出,以後每個結點前有一個空格 
36                  {
37                       cout <<j;
38                       t=1;
39                  }
40                  else
41                  {
42                      cout <<" "<<j;
43                  
44                  }
45            }
46            if (n[j].leftchild!=-1)
47            {
48                 q.push(n[j].leftchild);  //左兒子存在,壓入佇列 
49            }
50            if (n[j].rightchild!=-1)
51            {
52                  q.push(n[j].rightchild);//左右子存在,壓入佇列
53            }
54      }
55 }
56 int main()
57 {
58     char a,b;
59     int m;
60     while (cin>>m)
61     {
62           memset(r,false,sizeof(r));
63           for (int i=0;i<m;i++)//根據輸入資料構建樹 
64           {
65                 cin>>a>>b;
66                 if (a>='0'&&a<='9')
67                 {
68                       n[i].leftchild=a-48;
69                       r[a-48]=true;
70                 }
71                 if (b>='0'&&b<='9')
72                 {
73                       n[i].rightchild=b-48;
74                       r[b-48]=true;
75                 }
76           }
77           for (int i=0;i<m;i++)
78           {
79               if (!r[i])
80               {
81                   traversal(i);
82                   //cout <<"i"<<i<<endl;
83                   cout <<endl;
84                   break;
85               }      
86           }
87     }
88     return 0;
89 }