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.反转二叉树中序输出: 就是将一颗未反转的二叉树 通过右 中 左的 方式遍历输出,(相当于已经反转后的树的中序遍历)
2.至于层序输出再递归输出二叉树时,添加一个索引下标 只不过此时右结点的索引为 2 * n + 1 左结点的索引为 2 * n + 2
然后通过索引从小到大排序就是层序遍历;(如果依然是左结点的下标索引为 2 * n + 1, 右结点下标索引为 2 * n + 2 那么排序时将先通过高度从低到高排序 然后将每层的索引通过从大到小排序 然后输出结点就是层序遍历)
3.对于遍历的前提就是构建二叉树 并找到树的根;
构建一个 isRoot bool类型的数组判断是否为树的根,如果是,它的下标就是树的根id,判断的依据是此根结点不是任何结点的子节点;
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Node{
int id, index, l, r; //存储树的结点id(排序后下标发生改变),索引位置(方便层序遍历),左结点Id,//右结点id
}node[10];
int n;
bool isRoot[10]; //判断是否是根结点
vector<Node> in, level;
void inOrder(int root, int index){ //反向中序遍历 此时右结点的下标未index * 2 + 1;
if(root == -1) return;
node[root].index = index;
inOrder(node[root].r, index * 2 + 1);
in.push_back(node[root]);
inOrder(node[root].l, index * 2 + 2);
}
bool cmp(Node a, Node b){
return a.index < b.index; //根据索引从小到达排序
}
int main(){
cin >> n;
string l, r;
int root = 0;
fill(isRoot, isRoot + 10, true);
for(int i = 0; i < n; i++){
cin >> l >> r;
node[i].id = i;
if(l == "-"){
node[i].l = -1;
}else{
int left = stoi(l);
node[i].l = left;
isRoot[left] = false;
}
if(r == "-"){
node[i].r = -1; //devc ==是不报错的
}else{
int right = stoi(r);
node[i].r = right;
isRoot[right] = false;
}
}
while(isRoot[root] == false){
root++;
}
inOrder(root, 0);
level = in;
sort(level.begin(), level.end(), cmp);
for(int i = 0; i < level.size(); i++){
if(i != 0)
printf(" ");
printf("%d", level[i].id);
}
printf("\n");
for(int i = 0; i < in.size(); i++){
if(i != 0)
printf(" ");
printf("%d", in[i].id);
}
return 0;
}
第二种方式: 不改变左树 右树的索引位置,先通过从小到大比较高度 再比较相同高度索引(从大到小)
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Node{
int id, index, l, r, height;
}node[10];
int n;
bool isRoot[10];
vector<Node> in, level;
void inOrder(int root, int index, int height){
if(root == -1) return;
node[root].index = index;
node[root].height = height;
inOrder(node[root].r, index * 2 + 2, height + 1);
in.push_back(node[root]);
inOrder(node[root].l, index * 2 + 1, height + 1);
}
bool cmp(Node a, Node b){
if(a.height != b.height)
return a.height < b.height;
else
return a.index > b.index;
}
int main(){
cin >> n;
string l, r;
int root = 0;
fill(isRoot, isRoot + 10, true);
for(int i = 0; i < n; i++){
cin >> l >> r;
node[i].id = i;
if(l == "-"){
node[i].l = -1;
}else{
int left = stoi(l);
node[i].l = left;
isRoot[left] = false;
}
if(r == "-"){
node[i].r = -1; //devc ==是不报错的
}else{
int right = stoi(r);
node[i].r = right;
isRoot[right] = false;
}
}
while(isRoot[root] == false){
root++;
}
inOrder(root, 0, 0);
level = in;
sort(level.begin(), level.end(), cmp);
for(int i = 0; i < level.size(); i++){
if(i != 0)
printf(" ");
printf("%d", level[i].id);
}
printf("\n");
for(int i = 0; i < in.size(); i++){
if(i != 0)
printf(" ");
printf("%d", in[i].id);
}
return 0;
}