PAT甲级-二叉树的遍历-1102 Invert a Binary Tree解题思路
1102 Invert a Binary Tree (25 分)
思路
翻转二叉树
后序遍历翻转即可,由于给出每个结点的左右儿子,所以这里用到二叉树的静态写法更加方便
这里有个坑,bool数组初始化为false才是有效的,别的效果不行,暂时不知道为什么
代码
#include<bits/stdc++.h>
using namespace std;const int maxn = 30;struct node{ //二叉树的静态写法,更简单实现int lchild,rchild;
}Node[maxn];bool isRoot[maxn]={false};
int n,num=0;void post_reverse(int root)
{if(Node[root].lchild!=-1)post_reverse(Node[root].lchild);if(Node[root].rchild!=-1)post_reverse(Node[root].rchild);int change = Node[root].lchild;Node[root].lchild = Node[root].rchild;Node[root].rchild = change;
}void pre_traverse(int root)
{if(Node[root].lchild!=-1)pre_traverse(Node[root].lchild);num+=1;if(num==n)cout<<root;elsecout<<root<<" ";if(Node[root].rchild!=-1)pre_traverse(Node[root].rchild);
}void level_traverse(int root)
{queue<int>Q;Q.push(root);while(!Q.empty()){int nnn = Q.front();Q.pop();if(Node[nnn].lchild!=-1)Q.push(Node[nnn].lchild);if(Node[nnn].rchild!=-1)Q.push(Node[nnn].rchild);num+=1;if(num==n)cout<<nnn;elsecout<<nnn<<" ";}
}
int main()
{cin>>n;char ch;for(int i=0;i<n;i++){cin>>ch;if(ch=='-'){Node[i].lchild = -1;}else{int input = ch-'0';Node[i].lchild = input;isRoot[input] =true;}cin>>ch;if(ch=='-'){Node[i].rchild = -1;}else{int input = ch-'0';Node[i].rchild = input;isRoot[input] =true;}}int root;for(int i=0;i<n;i++){if(!isRoot[i])root=i;}post_reverse(root);num = 0;level_traverse(root);cout<<endl;num = 0;pre_traverse(root);}
PAT甲级-二叉树的遍历-1102 Invert a Binary Tree解题思路相关推荐
- PAT甲级1102 Invert a Binary Tree:[C++题解]反转二叉树、递归
文章目录 题目分析 题目链接 题目分析 反转二叉树这道题目特别出名!!!,是因为Homebrew这款Mac上大火的软件的作者到google面试,就考了这道题.面试官说了下面这段话:你竟然连在白板上翻转 ...
- 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(甲级)
1102 Invert a Binary Tree (25分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- 1102 Invert a Binary Tree
题目来源:PAT (Advanced Level) Practice The following is from Max Howell @twitter: Google: 90% of our eng ...
- 1102 Invert a Binary Tree (25point(s))
1102 Invert a Binary Tree (25point(s)) The following is from Max Howell @twitter: Google: 90% of our ...
- 1102 Invert a Binary Tree (25 分)
1102 Invert a Binary Tree (25 分) The following is from Max Howell @twitter: Google: 90% of our engin ...
- 1102. Invert a Binary Tree (25)-PAT甲级真题
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 1102 Invert a Binary Tree 需再做
1. 题目的输入是,先给出结点总数N,然后N行给出的是值为x(0<=x<=N-1)的结点的左右结点的值,若不存在左/右结点,则值为 - . 2. 这一题我用动态链表没有做出来,根据参考书提 ...
- PAT甲级Invert a Binary Tree 柳神层序遍历的思路值得借鉴
1102 Invert a Binary Tree (25分) The following is from Max Howell @twitter: Google: 90% of our engine ...
最新文章
- 官宣!推动深圳大学、南科大创建“双一流”!
- python连接memcached
- VSCode生成.vue模版
- 利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开(转)
- L - Clock Master Gym - 102798L
- 按键检测框架单击-双击-连按
- Spring Boot 启动参数设置详解
- sql盲注 解决_sql盲注学习笔记
- 职业技能鉴定 八大工种 广州紧缺工种 培训考证
- 企业开票信息税号查询API接口服务
- c语言中string函数的作用是,c++中的string常用函数用法总结
- APISpace 汉字转拼音API 方便好用
- 人机融合智能时代的人心
- 服务器上qq邮件不能打开方式,QQ邮箱打不开是怎么回事?从QQ面板上打不开QQ邮箱怎么解决?...
- 超声波测深仪工作原理
- python加权求和_加权随机算法的python实现
- JAVA_抽象类和接口
- 这才不是我想看的《时间简史》
- MFC(CFile)文件操作
- 百度网盘下载限速解决