PAT 1102 Invert a Binary Tree
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
分析
用vector Left和right来记录每个节点的左孩子节点和右孩子节点,利用根节点不是任何节点的子节点来找到根节点root,在从根节点开始将节点依次插入树中,最后就是基本的层序遍历和中序遍历的实现了。
#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
vector<int> Left;
vector<int> Right;
int flag=1;
class node{
public:
int value;
node* left=NULL;
node* right=NULL;
node(int val):value(val), left(NULL), right(NULL){
}
};
void CreateTree(node* root){
if(Left[root->value]!=-1){
root->left=new node(Left[root->value]);
CreateTree(root->left);
}
if(Right[root->value]!=-1){
root->right=new node(Right[root->value]);
CreateTree(root->right);
}
}
void inorder(node* root){
if(root->left)
inorder(root->left);
flag++==1?cout<<root->value:cout<<" "<<root->value;
if(root->right)
inorder(root->right);
}
int main(){
int N;
cin>>N;
vector<int> record(N,0);
Left.resize(N);
Right.resize(N);
for(int i=0; i<N; i++){
char l, r;
cin>>l>>r;
if(isdigit(l)){
Right[i]=l-'0';
record[Right[i]]=1;
}
else
Right[i]=-1;
if(isdigit(r)){
Left[i]=r-'0';
record[Left[i]]=1;
}
else
Left[i]=-1;
}
int i;
for(i=0; i<N; i++)
if(record[i]==0)
break;
node* root=new node(i);
CreateTree(root);
queue<node*> q;
q.push(root);
while(q.size()!=0){
node* t=q.front();
q.pop();
flag++==1?cout<<t->value:cout<<" "<<t->value;
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
}
cout<<endl;
flag=1;
inorder(root);
return 0;
}
PAT 1102 Invert a Binary Tree的更多相关文章
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]
题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT (Advanced Level) 1102. Invert a Binary Tree (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
随机推荐
- 怎样编辑LRC歌词
恭喜恭喜歌词 唐嫣 - 音乐巴士 http://www.yy8844.cn/geci/mswcn/nvunns.shtml 恭喜恭喜歌词感谢 音乐巴士 珍妮 编辑歌词匹配时间为: 03 分 06 秒 ...
- js如何获取某id的子标签
思路:根据id获取父对象,然后使用childNodes获取所有子对象数组,关键代码: document.getElementById(div_id).childNodes; // 子对象数组 实例 ...
- yistack
- Scala 获取当前时间
def NowDate(): String = { val now: Date = new Date() val dateFormat: SimpleDateFormat = new SimpleDa ...
- 面试题:InnoDB中一棵B+树能存多少行数据?
阅读本文大概需要 5 分钟. 作者:李平 | 来源:个人博客 一.InnoDB 一棵 B+ 树可以存放多少行数据? InnoDB 一棵 B+ 树可以存放多少行数据? 这个问题的简单回答是:约 2 千万 ...
- 【题解】动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393]
[题解]动态逆序对 [CQOI2011] [P3157] [BZOJ3295] [P1393] 水一水QAQ 题目链接: \([P3157]\) \([BZOJ3295]\) [题目描述] 对于一个序 ...
- applicationContext.xml配置AOP切面编程
Computer.java package com.wh.aop2; public class Computer { public void play01(){ System.out.println( ...
- 【LeetCode】467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- <stddef.h>
Common definitions 定义类型: ptrdiff_t 两指针相减的结果,signed integer size_t sizeof操作符的结果,unsigned integer max_ ...
- JVM 内存分配和垃圾回收(GC)机制
一 判断对象是否存活 垃圾收集器在对堆进行回收前,第一件事情就是要确定这些对象之中哪些还“活着”,哪些已经"死去”,即不能再被任何途径使用的对象. 1.1 引用计数法 (Reference ...