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 6Sample Output:
3 7 2 6 4 0 5 1 6 5 7 4 3 2 0 1
/**
作者:一叶扁舟
时间:19:43 2017/7/7
思路:
交换左右子树,并且层序输出和中序输出
例子:
*/
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
#define SIZE 101
typedef struct TreeNode{
int data;
int leftChild;
int rightChild;
}TreeNode;
int root[SIZE] = { 0 };//默认都是根节点
TreeNode treeNode[SIZE];//全局变量,存储树
int midNum = 0;//记录中序遍历输出的个数
int N;//总共的结点个数
//根据输入的字符,获取对应父节点的编号
int getNodeNum(char ch){
if (ch == '-'){
return -1;//没有该节点
}
else{
int temp = ch - '0';
root[temp] = 1;//temp值出现了,就说明该节点不是根节点,
//因为根节点是不可能有父节点的
return temp;
}
}
//找到根节点
int getRoot(int *root,int N){
for (int i = 0; i < N; i++){
if (root[i] != 1){//根节点默认为0了
return i;
}
}
return -1;
}
//层次遍历
void levelOrder(int root,int N){
queue<int> q;
q.push(root);
int num = 0;
while (!q.empty()){
int nodeNum = q.front();
q.pop();
num++;
if (treeNode[nodeNum].leftChild != -1){
q.push(treeNode[nodeNum].leftChild);
}
if (treeNode[nodeNum].rightChild != -1){
q.push(treeNode[nodeNum].rightChild);
}
printf("%d", treeNode[nodeNum].data);
if (num != N ){
printf(" ");
}
}
}
//中序遍历
void middleOrder( int root){
if (root != -1){
middleOrder(treeNode[root].leftChild);
printf("%d", treeNode[root].data);
midNum++;
if (midNum != N ){
printf(" ");
}
middleOrder( treeNode[root].rightChild);
}
return;
}
void exchangeNode( int rootNum){
if (rootNum == -1){
return;
}
int left = treeNode[rootNum].leftChild;
int right = treeNode[rootNum].rightChild;
treeNode[rootNum].leftChild = right;
treeNode[rootNum].rightChild = left;
exchangeNode(left);
exchangeNode(right);
}
int main(){
scanf("%d", &N);
for (int i = 0; i < N; i++){
char ch1, ch2;
//%*c吸收掉换行符
scanf("%*c%c %c", &ch1, &ch2);
int num1 = getNodeNum(ch1);
int num2 = getNodeNum(ch2);
treeNode[i].data = i;
treeNode[i].leftChild = num1;
treeNode[i].rightChild = num2;
}
//找到根节点
int rootNum = getRoot(root, N);
//交换左右子树
exchangeNode( rootNum);
//层次遍历
levelOrder(rootNum, N);
printf("\n");
//中序遍历
middleOrder( rootNum);
printf("\n");
system("pause");
return 0;
}