PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042

1102 Invert a Binary Tree (25 分)
 

The following is from Max Howell @twitter:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

Now it's your turn to prove that YOU CAN invert a binary tree!

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 from 0 to N1, 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 the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. 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:

3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1

题目大意:将二叉树每个节点的左右孩子交换位置,然后分别层序和中序输出。第 i 行代表了节点 i 的左右孩子的信息,先左后右,'-' 表示没有该方向的子节点。

思路:用数组tree存储树。

读取字符串然后将其转成int型变量,空节点'-' 用-1代替。

只要在读取数据的时候交换左右孩子的位置就行。

读取数据的时候用bool数组R对每个孩子节点进行标记,然后遍历数组寻找根节点(即没有被标记过的节点)

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <queue>
 5 using namespace std;
 6 struct node {
 7     int left, right;
 8 };
 9 vector <node> tree;
10 bool flag = false;//用于中序遍历标记第一个输出的节点
11 int getNum(string &s);
12 void levelOrder(int t);
13 void inOrder(int t);
14 int main()
15 {
16     int N, root;
17     scanf("%d", &N);
18     tree.resize(N);
19     vector <bool> R(N, true);
20     for (int i = 0; i < N; i++) {
21         string left, right;
22         cin >> left >> right;
23         tree[i].left = getNum(right);
24         tree[i].right = getNum(left);
25         if (tree[i].left != -1)
26             R[tree[i].left] = false;
27         if (tree[i].right != -1)
28             R[tree[i].right] = false;
29     }
30     for (int i = 0; i < N; i++)
31         if (R[i]) {
32             root = i;
33             break;
34         }
35     levelOrder(root);
36     printf("
");
37     inOrder(root);
38     printf("
");
39     return 0;
40 }
41 void inOrder(int t) {
42     if (t != -1) {
43         inOrder(tree[t].left);
44         if (flag)
45             printf(" ");
46         if (!flag)
47             flag = true;
48         printf("%d", t);
49         inOrder(tree[t].right);
50     }
51 }
52 void levelOrder(int t) {
53     queue <int> Q;
54     Q.push(t);
55     while (!Q.empty()) {
56         t = Q.front();
57         printf("%d", t);
58         Q.pop();
59         if (tree[t].left != -1) {
60             Q.push(tree[t].left);
61         }
62         if (tree[t].right != -1) {
63             Q.push(tree[t].right);
64         }
65         if (!Q.empty())
66             printf(" ");
67     }
68 }
69 int getNum(string& s) {
70     if (s[0] == '-')
71         return -1;
72     int n = 0;
73     for (int i = 0; i < s.length(); i++)
74         n = n * 10 + s[i] - '0';
75     return n;
76 }
原文地址:https://www.cnblogs.com/yinhao-ing/p/10926383.html