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 (≤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 from 0 to N−1, 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
思路:由于题目直接给出了节点编号关系,因此使用二叉树的静态写法非常方便。处理输入问题,如果是数字,直接赋值给该节点的孩子,否则不进行操作,默认为-1;建树完成后可以使用先序遍历或者是后序遍历进行树的反转,改变原来树的结构,随后就是简单的遍历操作啦。
#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 100;
struct node {//二叉树静态写法int lchild=-1, rchild=-1;
}no[maxn];
int n;
int in_flag = 0, le_flag = 0;//在中序遍历和层序遍历时控制格式;
void postorder(int root)//后序遍历进行反转
{if (root==-1){return;}postorder(no[root].lchild);postorder(no[root].rchild);swap(no[root].lchild, no[root].rchild);//交换左右孩子,反转;
}
void inorder(int root)//中序遍历二叉树
{if (root == -1){return;}inorder(no[root].lchild);if (in_flag++ == 0)cout << root;else cout << " " <<root;inorder(no[root].rchild);
}
void level(int root)//层序遍历二叉树
{queue<int>q;q.push(root);int le_flag = 0;while (!q.empty()){int tmp = q.front();q.pop();if (le_flag++ == 0)cout << tmp;else cout << " " << tmp;if (no[tmp].lchild != -1)q.push(no[tmp].lchild);if (no[tmp].rchild != -1)q.push(no[tmp].rchild);}}
int main()
{int fa[11]{ 0 };//记录每个节点是否有父节点cin >> n;for (int i = 0; i < n; i++)//建立静态二叉树{string str1, str2;cin >> str1 >> str2;if (str1 != "-"){no[i].lchild = stoi(str1);fa[no[i].lchild] = 1;}if (str2 != "-"){no[i].rchild = stoi(str2);fa[no[i].rchild] = 1;}}int i = 0;for (i; i < n; i++)//寻找根节点,根节点的父节点不存在{if (fa[i] == 0){break;}}postorder(i);level(i);cout << endl;inorder(i);
}
也可以不对树进行改变,因为题目只是需要正确的输出就行,我们就可以只模拟输出。
#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 100;
struct node {//二叉树静态写法int lchild=-1, rchild=-1;
}no[maxn];
int n;
int in_flag = 0, le_flag = 0;//在中序遍历和层序遍历时控制格式;
void inorder(int root)//不进行节点交换,先遍历右子树
{if (root==-1){return;}inorder(no[root].rchild);if (in_flag++ == 0)cout << root;else cout << " " << root;inorder(no[root].lchild);
}
void level(int root)//层序遍历二叉树
{queue<int>q;q.push(root);int le_flag = 0;while (!q.empty()){int tmp = q.front();q.pop();if (le_flag++ == 0)cout << tmp;else cout << " " << tmp;if (no[tmp].rchild != -1)q.push(no[tmp].rchild);//从右往左pushif (no[tmp].lchild != -1)q.push(no[tmp].lchild);//}}
int main()
{int fa[11]{ 0 };//记录每个节点是否有父节点cin >> n;for (int i = 0; i < n; i++)//建立静态二叉树{string str1, str2;cin >> str1 >> str2;if (str1 != "-"){no[i].lchild = stoi(str1);fa[no[i].lchild] = 1;}if (str2 != "-"){no[i].rchild = stoi(str2);fa[no[i].rchild] = 1;}}int i = 0;for (i; i < n; i++)//寻找根节点,根节点的父节点不存在{if (fa[i] == 0){break;}}level(i);cout << endl;inorder(i);}