PAT (Advanced Level) Practise 1102 Invert a Binary Tree PAT 1102 坑点
2016-05-05 21:01
711 查看
题目链接: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
解释
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; }
相关文章推荐
- leetcode 92 Reverse Linked List II C++
- C语言内存分配方式
- More Effective C++----异常 & (9)使用析构函数防止资源泄漏
- C++中引用类型
- [Assignment] C++1
- 【C/C++】类中的static成员、static const成员和const成员
- [C/C++] main函数执行前后还会发生什么
- Effective C++ 改善程序与设计的55个具体做法之条款37
- C语言中字符串的格式化
- c语言
- C++异常
- hashtable笔记 c++
- 数组型队列(queue)的使用(支持自定义数据类型)(C++版)
- Leetcode 17. Letter Combinations of a Phone Number
- C++第五次实验-项目1:数组分离
- leetcode 206 Reverse Linked List C++
- 标准C++中的string类的用法总结
- Effective C++ 改善程序与设计的55个具体做法之条款33
- C++中 #ifdef 和#endif的作用
- Java语法:与c++不同的地方