PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历。
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <string.h>
- #include <cstdio>
- #include <queue>
- using namespace std;
- const int maxn=;
- bool first=true;
- struct Node{
- int id;
- int left;
- int right;
- }node[maxn];
- int vis[maxn]; //没出现的即是根节点
- //层次遍历
- void level_order(int i){
- queue<Node>q;
- q.push(node[i]);
- Node tmp;
- while(!q.empty()){
- tmp=q.front();
- q.pop();
- if(first){
- first=false;
- printf("%d",tmp.id);
- }
- else
- printf(" %d",tmp.id);
- int l=tmp.left;
- int r=tmp.right;
- if(l!=-)
- q.push(node[l]);
- if(r!=-)
- q.push(node[r]);
- }
- }
- //中序遍历
- void in_order(int i){
- if(i==-)
- return;
- int l=node[i].left;
- int r=node[i].right;
- in_order(l);
- if(first){
- printf("%d",i);
- first=false;
- }
- else{
- printf(" %d",i);
- }
- in_order(r);
- }
- int main()
- {
- int n;
- char str1[],str2[];
- memset(vis,,sizeof(vis));
- scanf("%d",&n);
- for(int i=;i<n;i++){
- scanf("%s %s",str1,str2);
- node[i].id=i;
- if(str1[]=='-')
- node[i].right=-;
- else{
- node[i].right=str1[]-'';
- vis[str1[]-'']=;
- }
- if(str2[]=='-')
- node[i].left=-;
- else{
- node[i].left=str2[]-'';
- vis[str2[]-'']=;
- }
- }
- int root;
- for(int i=;i<n;i++){
- if(!vis[i]){
- root=i;
- break;
- }
- }
- first=true;
- level_order(root);
- printf("\n");
- first=true;
- in_order(root);
- return ;
- }
PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)的更多相关文章
- 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 (Advanced Level) 1102. Invert a Binary Tree (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- 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 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
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT甲题题解-1124. Raffle for Weibo Followers-模拟,水题
水题一个,贴个代码吧. #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
随机推荐
- MSChart 设置饼图颜色 图例背景色 图例显示位置
chartField.Series.Clear(); chartField.ChartAreas.Clear(); chartField.Legends.C ...
- PowerShell “execution of scripts is disabled on this system.”
Set-ExecutionPolicy RemoteSigned
- [HZNOI #koishi] Magic
[HZNOI #514] Magic 题意 给定一个 \(n\) 个点 \(m\) 条边的有向图, 每个点有两个权值 \(a_i\) 和 \(b_i\), 可以以 \(b_i\) 的花费把第 \(i\ ...
- Scala学习之路 (九)Scala的上界和下届
一.泛型 1.泛型的介绍 泛型用于指定方法或类可以接受任意类型参数,参数在实际使用时才被确定,泛型可以有效地增强程序的适用性,使用泛型可以使得类或方法具有更强的通用性.泛型的典型应用场景是集合及集合中 ...
- ethereum/EIPs-725
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-725.md eip title author discussions-to status ...
- C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈
研究了一下KeyDown,KeyPress 和 KeyUp 的学问.让我们带着如下问题来说明: 1.这三个事件的顺序是怎么样的? 2.KeyDown 触发后,KeyUp是不是一定触发? 3.三个事件的 ...
- C#自定义控件开发
自定义控件开发 一般而言,Visual Studio 2005中自带的几十种控件已经足够我们使用了,但是,在一些特殊的需求中,可能需要一些特殊的控件来与用户进行交互,这时,就需要我们自己开发新的.满足 ...
- Python2.7-SciPy
SciPy函数库在NumPy库的基础上增加了众多的数学.科学以及工程计算中常用的库函数.例如线性代数.常微分方程数值求解.信号处理.图像处理.稀疏矩阵等等 1.最小二乘拟合 详细介绍:https:// ...
- day38
今日内容: 1.认识数据库 2.修改默认密码 3.常用操作指令 1.认识数据库 什么是MYSQL? 是一个关系型数据库管理系统,基于socket编写的C/S架构的软件 什么是数据库? 数据:用于记录事 ...
- Jquery属性练习
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...