List Leaves

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

 

解题思路

  这道题目算是比较基础的。树是存储到静态链表中的,而且只存储左右孩子的下标,没有存储根节点的下标。关键的地方就在于如何找到根节点的下标位置?

  我们知道,整一颗树的根节点是不可能成为其他节点的孩子节点的。这意味着,在静态链表中所存储的左右孩子的下标是不包括根节点的下标的。所以我们们可以把整个静态链表遍历一遍,找到没有出现过的下标位置,那么这个下标就对应着根节点的下标。

  找到了根节点的下标位置后,剩下的只需要用一个队列来层次遍历就可以实现从上到下,左到右的顺序来输出叶子节点了。

  我用了两种代码,其实本质是一样的。只不过第一次是自己用类来创建一个很简陋的队列。

  AC代码如下:

 1 #include <cstdio>
 2 
 3 struct TNode {
 4     int left;
 5     int right;
 6 }T[10];
 7 
 8 class Queue {
 9 public:
10     int *data;
11     int front;
12     int rear;
13     
14 public:
15     Queue() {
16         this -> data = new int[10];
17         this -> front = this -> rear = 0;
18     }
19     void enQueue(int root) {
20         this -> data[this -> rear++] = root;
21     }
22     int deQueue() {
23         return this -> data[this -> front++];
24     }
25     bool isEmpty() {
26         return this -> front == this -> rear;
27     }
28     ~Queue() {
29         if (data) {
30             delete data;
31             data = NULL;
32         }
33     }
34 };
35 
36 int creatTree();
37 void leveleTraversal(int root);
38 
39 int main() {
40     int root = creatTree();
41     leveleTraversal(root);
42     
43     return 0;
44 }
45 
46 int creatTree() {
47     int n;
48     scanf("%d", &n);
49     
50     char ch1, ch2;
51     bool isFind[n] = {false};
52     for (int i = 0; i < n; i++) {
53         scanf("\n%c %c", &ch1, &ch2);
54         
55         if (ch1 == '-') T[i].left = -1;
56         else {
57             T[i].left = ch1 - '0';
58             isFind[T[i].left] = true;
59         }
60         
61         if (ch2 == '-') T[i].right = -1;
62         else {
63             T[i].right = ch2 - '0';
64             isFind[T[i].right] = true;
65         }
66     }
67     
68     int index = 0;
69     while (index < n && isFind[index]) {
70         index++;
71     }
72     
73     return index;
74 }
75 
76 void leveleTraversal(int root) {
77     Queue Q;
78     Q.enQueue(root);
79     
80     bool flag = false;
81     while (!Q.isEmpty()) {
82         int r = Q.deQueue();
83         
84         if (T[r].left == -1 && T[r].right == -1) {
85             if (flag) putchar(' ');
86             printf("%d", r);
87             flag = true;
88         }
89         
90         if (T[r].left != -1) Q.enQueue(T[r].left);
91         if (T[r].right != -1) Q.enQueue(T[r].right);
92     }
93 }

  第二份代码用了STL中的queue,使用起来方便多了。

 1 #include <cstdio>
 2 #include <queue>
 3 
 4 struct TNode {
 5     int left;
 6     int right;
 7 }T[10];
 8 
 9 int creatTree();
10 void leveleTraversal(int root);
11 
12 int main() {
13     int root = creatTree();
14     leveleTraversal(root);
15     
16     return 0;
17 }
18 
19 int creatTree() {
20     int n;
21     scanf("%d", &n);
22     
23     char ch1, ch2;
24     bool isFind[n] = {false};
25     for (int i = 0; i < n; i++) {
26         scanf("\n%c %c", &ch1, &ch2);
27         
28         if (ch1 == '-') T[i].left = -1;
29         else {
30             T[i].left = ch1 - '0';
31             isFind[T[i].left] = true;
32         }
33         
34         if (ch2 == '-') T[i].right = -1;
35         else {
36             T[i].right = ch2 - '0';
37             isFind[T[i].right] = true;
38         }
39     }
40     
41     int index = 0;
42     while (index < n && isFind[index]) {
43         index++;
44     }
45     
46     return index;
47 }
48 
49 void leveleTraversal(int root) {
50     std::queue<int> Q;
51     Q.push(root);
52     
53     bool flag = false;
54     while (!Q.empty()) {
55         int r = Q.front();
56         Q.pop();
57         
58         if (T[r].left == -1 && T[r].right == -1) {
59             if (flag) putchar(' ');
60             printf("%d", r);
61             flag = true;
62         }
63         
64         if (T[r].left != -1) Q.push(T[r].left);
65         if (T[r].right != -1) Q.push(T[r].right);
66     }
67 }

posted @ 2021-03-26 08:18  onlyblues  阅读(97)  评论(0编辑  收藏  举报
Web Analytics