题目链接:http://www.patest.cn/contests/pat-a-practise/1102
- Invert a Binary Tree (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
下面的代码case 1也就是测试点 1不能通过
#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
vector< int > Btree[13];
int isNotFirst = 0;
void levelOrder(int start){
deque<int> q;
q.push_back(start);
printf("%d",start);
while(!q.empty()){
int t = q.front();
q.pop_front();
for(int i=0;i<Btree[t].size();i++){
if(Btree[t][i] != -1){
printf(" %d",Btree[t][i]);
q.push_back(Btree[t][i]);
}
}
}
return;
}
void inOrder(int start){
if(start == -1){
return ;
}
inOrder(Btree[start][0]);
if(isNotFirst){
printf(" ");
}else{
isNotFirst=1;
}
printf("%d",start);
inOrder(Btree[start][1]);
return ;
}
int main()
{
int N;
scanf("%d",&N);
getchar();
int root=0;
for(int i=0;i<N;i++){
char rightC;
char leftC;
scanf("%c %c",&leftC,&rightC);
getchar();
if(rightC != '-'){
Btree[i].push_back(rightC -'0');
if(rightC -'0' == root){
root =i;
}
}else{
Btree[i].push_back(-1);
}
if(leftC != '-'){
Btree[i].push_back(leftC -'0');
if(leftC -'0' == root){
root = i;
}
}else{
Btree[i].push_back(-1);
}
}
levelOrder(root);
printf("\n");
inOrder(root);
return 0;
}
解释
根节点root计算有问题,比如输入是:
3
- -
- 1
- 0
正确AC代码
#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
vector< int > Btree[13];
int treeRoot[13];
int isNotFirst = 0;
void levelOrder(int start){
deque<int> q;
q.push_back(start);
printf("%d",start);
while(!q.empty()){
int t = q.front();
q.pop_front();
for(int i=0;i<Btree[t].size();i++){
if(Btree[t][i] != -1){
printf(" %d",Btree[t][i]);
q.push_back(Btree[t][i]);
}
}
}
return;
}
void inOrder(int start){
if(start == -1){
return ;
}
inOrder(Btree[start][0]);
if(isNotFirst){
printf(" ");
}else{
isNotFirst=1;
}
printf("%d",start);
inOrder(Btree[start][1]);
return ;
}
int main()
{
fill(treeRoot,treeRoot+13,1);
int N;
scanf("%d",&N);
getchar();
int root=0;
for(int i=0;i<N;i++){
char rightC;
char leftC;
scanf("%c %c",&leftC,&rightC);
getchar();
if(rightC != '-'){
Btree[i].push_back(rightC -'0');
treeRoot[rightC -'0']=0;
}else{
Btree[i].push_back(-1);
}
if(leftC != '-'){
Btree[i].push_back(leftC -'0');
treeRoot[leftC -'0']=0;
}else{
Btree[i].push_back(-1);
}
}
for(int i=0;i<N;i++){
if(treeRoot[i] ==1){
root = i;
break;
}
}
levelOrder(root);
printf("\n");
inOrder(root);
return 0;
}