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
题意:
二叉树有N个结点,给出每个结点的孩子结点的编号,把二叉树反转,输出反转后二叉树的层次遍历序列和中序遍历序列。
样例解释:
8 表示一个八个节点
1 - 0号结点的左孩子是1,没有右孩子
思路:
1.题目给出的是结点编号的关系,因此静态二叉树会比较方便。
2.输入的时候,如果是"-"表示没有孩子结点,用-1表示,如果是数字,直接把lchild和rchild记为该数字就可以。同时,需要找到这个二叉树的根结点,找一个结点,它不是任何结点的孩子即可。开一个bool型的notRoot数组,在输入时看看哪个结点没有作为其他结点的孩子出现,若出现则置为true,表示它不是根结点。然后遍历notRoot数组,若notRoot[i]==false即表示i是根结点。
3.二叉树反转可以通过后序遍历实现(在后序遍历访问根结点时交换lchild和rchild)
注意:
scanf的%c格式会读入换行符,所以需要在每行输入前把上一行的换行符吸收掉,**可以使用getchar(),或者用scanf("%c "),用%c就可以在scanf中接收一个字符。
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 110;
struct node
{
int lchild;
int rchild;
}Node[maxn];
bool notRoot[maxn] = {false}; //初始化,都记为根结点
int n, num = 0; //结点个数,已输出结点的个数
void print(int id) //输出结点id的编号
{
printf("%d", id); //输出id
num++;
if(num < n)
printf(" "); //最后一个结点不输出空格
else
printf("\n"); //全部输出完后换行
}
void inOrder(int root) //中序遍历
{
if(root == -1) return;
inOrder(Node[root].lchild);
print(root);
inOrder(Node[root].rchild);
}
//层次遍历
void BFS(int root)
{
queue<int> q; //注意队列里存的是地址
q.push(root); //根结点地址入队
while(!q.empty())
{
int top = q.front(); //取出队首元素
q.pop();
print(top); //输出队首元素(树的总根结点)
if(Node[top].lchild != -1) //当前结点的左子树非空,注意里面是top不是root(因为是当前结点,也就是队首结点top)
q.push(Node[top].lchild);
if(Node[top].rchild != -1) //当前结点的右子树非空
q.push(Node[top].rchild);
}
}
void postOrder(int root) //后序遍历,用来反转二叉树
{
if(root == -1) return;
postOrder(Node[root].lchild);
postOrder(Node[root].rchild);
swap(Node[root].lchild, Node[root].rchild); //交换左右孩子结点
}
int strToNum(char c) //将输入的字符转成-1或者结点编号
{
if(c == '-') return -1; // "-"表示没有孩子结点,记为-1
else
{
notRoot[c - '0'] = true; //标记根结点不是c
return c - '0'; //返回结点编号
}
}
int findRoot() //寻找根结点编号
{
for(int i = 0; i < n; i++)
if(notRoot[i] == false) //是根结点
return i;
}
int main()
{
char lchild, rchild;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
getchar(); //吸收回车符
scanf("%c %c", &lchild, &rchild); //%c读入格式会将空格读入 所以要%c %c
// scanf("%*c %c %c", &lchild, &rchild);
Node[i].lchild = strToNum(lchild);
Node[i].rchild = strToNum(rchild);
}
int root = findRoot(); //获得根结点编号
postOrder(root); //后序遍历,反转二叉树
BFS(root); //输出层次遍历序列
num = 0; //将已输出结点个数置0
inOrder(root); //输出中序遍历序列
return 0;
}