当前位置:网站首页>【刷题篇】二叉树的右视图
【刷题篇】二叉树的右视图
2022-08-03 21:33:00 【m0_60631323】
一、题目
OJ链接
请根据二叉树的前序遍历,中序遍历恢复二叉树,并打印出二叉树的右视图
示例:
对应的输出为[1,3,5]。
二、题解
解决本题主要 分两个步骤:
- 根据前序和中序构建出二叉树
- 对二叉树进行层次遍历,找到每一层的最右侧的节点
构建二叉树:
参考链接: 二叉树相关OJ题
中序遍历找出最每一层最右侧的节点:
层序遍历的过程
变量size记录的是当前遍历到的层的节点数,我们在入队的时候的顺序是:从左到右依次将当前层的节点加入到队列中,所以在出队的时候,当前层的最右侧的节点一定是该层所有节点中最后一个出队列的
故我们可根据这一特点,增设一个判断条件,但size为0时,此时上一次出的元素就是当前层的最右侧的元素
源码:
public class Solution {
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 求二叉树的右视图 * @param xianxu int整型一维数组 先序遍历 * @param zhongxu int整型一维数组 中序遍历 * @return int整型一维数组 */
int preIndex=0;
HashMap<Integer,Integer> map=new HashMap<>();
public void RootIndex(int[] inorder){
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i],i);
}
}
public ArrayList<Integer> rightSideView(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
while(!q.isEmpty()){
//队列中的大小即是这一层的节点树
int size = q.size();
while(size-- != 0){
TreeNode temp = q.poll();
if(temp.left != null)
q.offer(temp.left);
if(temp.right != null)
q.offer(temp.right);
//最右元素
if(size == 0)
res.add(temp.val);
}
}
return res;
}
public int[] solve (int[] xianxu, int[] zhongxu) {
TreeNode root=buildTree(xianxu,zhongxu);
ArrayList<Integer> tmp=rightSideView(root);
int[] arr=new int[tmp.size()];
for(int i=0;i<tmp.size();i++){
arr[i]=tmp.get(i);
}
return arr;
}
public TreeNode buildTree(int[] xianxu,int[] zhongxu ){
RootIndex(zhongxu);
return buildTreeChild(xianxu,zhongxu,0,zhongxu.length-1);
}
public TreeNode buildTreeChild(int[] xianxu,int[] zhongxu,int inbegin,int inend){
if(inbegin>inend){
return null;
}
TreeNode root=new TreeNode(xianxu[preIndex]);
int ri=map.get(xianxu[preIndex]);
preIndex++;
root.left=buildTreeChild(xianxu,zhongxu,inbegin,ri-1);
root.right=buildTreeChild(xianxu,zhongxu,ri+1,inend);
return root;
}
}
边栏推荐
- 上课笔记(6)(1)——#629. 表达式括号匹配(stack)
- 2021年数据泄露成本报告解读
- Engineering Effectiveness Governance for Agile Delivery
- 4. 模块化编程
- 肝完 Alibaba 这份面试通关宝典,我成功拿下今年第 15 个 Offer
- 小朋友学C语言(1):Hello World
- C. Keshi Is Throwing a Party- Codeforces Global Round 17
- 《强化学习周刊》第56期:GraphIRL、REDEEMER & 眼科强化学习的潜在研究
- Several difficult problems in DDD
- 太香了! 阿里 Redis 速成笔记, 从头到尾全是精华!
猜你喜欢
随机推荐
基于支持向量机的网络⼊侵检测系统的全面调查和分类
开源一夏 |如何优化线上服务器
False label aggregation
CAS: 773888-45-2_BIOTIN ALKYNE_生物素-炔基
win10安装及配置Gradle
2022年全国职业院校技能大赛网络安全 B模块 任务十windows操作系统渗透测试 国赛原题
从开发到软件测试:除了扎实的测试基础,还有哪些必须掌握 ?
这几个常用 alias,带你高效做事(下)
StoneDB 开源社区月刊 | 202207期
LyScript 实现应用层钩子扫描器
一体化HTAP数据库如此难,为什么他们还要做?
CAS:1260586-88-6_Biotin-C5-Azide_Biotin-C5-Azide
关于GPIO你真的懂了吗?这篇文章都给你整理好了
『百日百题 · 基础篇』备战面试,坚持刷题 第四话——循环语句!
XSS online shooting range---prompt
2022年全国职业院校技能大赛网络安全 B模块 B-1任务一:主机发现与信息收集 国赛原题
How to deal with commas in the content of the CSV file of the system operation and maintenance series
MMA安装及使用优化
FVCOM 3D Numerical Simulation of Hydrodynamics, Water Exchange, Dispersion and Transport of Oil Spills丨FVCOM Model Watershed, Numerical Simulation Method of Marine Water Environment
XSS测试



![[3D检测系列-PV-RCNN] PV-RCNN论文详解、PV-RCNN代码复现、包含官网PV-RCNN预训练权重及报错问题](/img/81/c929864440dc36238b3cb1deb9f112.png)





