PAT甲级1102 Invert a Binary Tree:[C++题解]反转二叉树、递归
文章目录
- 题目分析
- 题目链接
题目分析
反转二叉树这道题目特别出名!!!,是因为Homebrew这款Mac上大火的软件的作者到google面试,就考了这道题。面试官说了下面这段话:你竟然连在白板上翻转二叉树都不会,还是滚吧。
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.
Homebrew简介
来源:维基百科
分析: 反转二叉树,这里用的递归的写法,交换根结点的左右子树。
先交换再遍历左右子树可以;
void dfs_reverse(int u){if(u == -1) return; //没有儿子返回swap(l[u],r[u]); //交换左右子树 dfs_reverse(l[u]); dfs_reverse(r[u]);
}
先遍历左右子树,再交换左右子树也可以。
void dfs_reverse(int u){if(u == -1) return;dfs_reverse(l[u]); dfs_reverse(r[u]);swap(l[u],r[u]); //交换左右子树}
ac代码
#include<bits/stdc++.h>
using namespace std;const int N = 20;int l[N] ,r[N];
int q[N]; //数组模拟队列
int n,root;bool has_father[N]; //看当前结点有无父节点,从而求根结点//翻转二叉树
void dfs_reverse(int u){if(u == -1) return;dfs_reverse(l[u]);dfs_reverse(r[u]);swap(l[u],r[u]); //交换左右子树}//层次遍历bfs
void bfs(int u){int hh = 0 ,tt = 0;q[0] =u;while(hh<= tt){int t =q[hh++];if(l[t] != -1) q[++tt] = l[t];if(r[t] != -1 ) q[++ tt] = r[t];}cout<< q[0];for(int i=1;i<n;i ++) cout<<" "<<q[i];cout<<endl;
}//中序遍历
/*k用来统计输出的个数,防止行末空格*/
void dfs( int u, int& k){//中序遍历if(u == -1) return;dfs(l[u], k);cout<< u;if(++k !=n) cout<<" ";dfs(r[u] , k);}int main(){//左右儿子数组初始化为-1,表示没有儿子memset(l,-1, sizeof l);memset(r ,-1, sizeof r);cin>> n;for(int i=0; i<n; i++) {char lc, rc;cin>> lc >> rc;if(lc != '-') l[i] = lc -'0',has_father[l[i]] =true;if(rc != '-') r[i] =rc - '0',has_father[r[i]] =true;}root =0;while(has_father[root]) root++;dfs_reverse(root); //反转二叉树bfs(root); //层次遍历int k =0; //防止行末空格dfs(root , k); //中序遍历
}
题目链接
PAT甲级1102 Invert a Binary Tree
PAT甲级1102 Invert a Binary Tree:[C++题解]反转二叉树、递归相关推荐
- 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 (Advanced Level) Practice The following is from Max Howell @twitter: Google: 90% of our eng ...
- PAT甲级-二叉树的遍历-1102 Invert a Binary Tree解题思路
1102 Invert a Binary Tree (25 分) 思路 翻转二叉树 后序遍历翻转即可,由于给出每个结点的左右儿子,所以这里用到二叉树的静态写法更加方便 这里有个坑,bool数组初始化为 ...
- 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 (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 ( ...
- PAT甲级1074 Reversing Linked List :[C++题解]反转链表,借用vector
文章目录 题目分析 题目链接 题目分析 分析:数组模拟链表,这题反转操作在数组中进行,然后直接输出即可,甚至不用放回到链表. //遍历链表,该链表用数组模拟 //保存链表结点地址到数组中 for(in ...
- 1102 Invert a Binary Tree 需再做
1. 题目的输入是,先给出结点总数N,然后N行给出的是值为x(0<=x<=N-1)的结点的左右结点的值,若不存在左/右结点,则值为 - . 2. 这一题我用动态链表没有做出来,根据参考书提 ...
最新文章
- python处理excel字典-使用Python代码处理Excel
- 什么是集成测试?非渐增式和渐增式有什么区别
- 都说了多少遍,不要再学 JSP 了!
- Engineering Department Bulletin News and information for the Department
- C++中4种强制类型转换 ?
- 解决IntelliJ IDEA报错Failed to read candidate component class: file [ ]; nested exception is org.
- PHP全栈开发(八):CSS Ⅹ 导航栏制作
- 亚信科技数据库AntDB通过金融分布式事务数据库标准测试
- OptaPlanner –具有真实道路距离的车辆路线
- python程序打包成安卓app教程_Python zipapp打包教程(超级详细)
- 有了HTML5,Flash还能走多远?
- 最新的中国姓氏重新洗牌:快来看看你的姓排第几?
- 计算机设置定时关机win10,win10简单设置定时关机,电脑设置每日定时关机
- 时序违例的原因及其解决办法
- 2020CADCG会议专题报告深度学习和图像笔记
- python爬虫爬取漫画(仅供学习)
- 科研ABC - SCI论文写作
- 爬取 bilibili 纪录片数据数据可视化
- 学会VBA,菜鸟也高飞!
- MongoDB快速实战与基本原理-01
热门文章
- 记一次死锁问题的排查和解决
- .net生成excel并弹出保存提示框(转载)
- mySql完全手册2011031401
- .net之 datagrid
- 用神经网络计算甲醛CH2O和亚硝酸HNO2的化学键的键能
- mysql服务器端口cpu_mysql导致服务器cpu100%的问题一例
- android自适应拉伸图片,Android 启动页-解决图片被拉伸和压缩问题,适配虚拟导航栏...
- 利用pip3安装包只能在python2中调用
- 一起学nRF51xx 13 - twi iic
- STM32 基础系列教程 19 – WWDG