当前位置:网站首页>牛客-TOP101-BM41
牛客-TOP101-BM41
2022-08-02 04:41:00 【一条吃猫的鱼】
import java.util.*;
public class Solution {
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 求二叉树的右视图 * @param xianxu int整型一维数组 先序遍历 * @param zhongxu int整型一维数组 中序遍历 * @return int整型一维数组 */
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
int n = pre.length;
int m = vin.length;
if(n == 0 || m == 0){
return null;
}
TreeNode t = new TreeNode(pre[0]);
for(int i = 0; i < vin.length; i++){
if(pre[0] == vin[i]){
t.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(vin, 0, i));
t.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(vin, i+1, vin.length));
break;
}
}
return t;
}
public ArrayList<Integer> rightSideView(TreeNode root){
ArrayList<Integer> list = new ArrayList<>();
Deque<TreeNode> deque = new LinkedList<>();
deque.offerLast(root);
while(!deque.isEmpty()){
int size = deque.size();
TreeNode t = null;
while(size != 0){
t = deque.pollFirst();
if(t.left != null)
deque.offerLast(t.left);
if(t.right != null)
deque.offerLast(t.right);
size--;
}
list.add(t.val);
}
return list;
}
public int[] solve (int[] xianxu, int[] zhongxu) {
// write code here
if(xianxu.length == 0 || zhongxu.length == 0){
return new int[0];
}
TreeNode root = reConstructBinaryTree(xianxu, zhongxu);
ArrayList<Integer> list = rightSideView(root);
int[] res = new int[list.size()];
for(int i = 0; i < list.size(); i++)
res[i] = list.get(i);
return res;
}
}
边栏推荐
猜你喜欢

UE4 3DUI显示与交互案例

Minecraft 1.18.1, 1.18.2 module development 23.3D animation armor production

论文速读:Homography Loss for Monocular 3D Object Detection

数据湖:流计算处理框架Flink概述

来自雪域高原的馈赠——大凉山高原生态糖心苹果

UE4 利用Mixamo自动绑骨并导入虚幻4

Learn about the sequential storage structure of binary tree - heap

redis基础入门

【C语言程序】求直角三角形边长

Does Conway's Law Matter for System Architecture?
随机推荐
ZCMU--1891: kotomi and game(C语言)
What if some fields don't want to be serialized?
【C语言程序】求直角三角形边长
线代005
如何运用3DGIS技术整合智慧社区综合管理解决方案
PyQt5_pyqtgraph mouse draws straight lines on line charts
gergovia的交易tijie
来自雪域高原的馈赠——大凉山高原生态糖心苹果
alibaba数据同步组件canal的实践整理
WordPress是什么?我也想用 WordPress~
开放原子开源峰会落幕,百度超级链牵头成立XuperCore开源工作组
【Interview】Recruitment requirements
洗牌(DAY 100)
P1012 [NOIP1998 Improve Group] Spelling
区间和 离散化
学内核之四:关于内核与硬件的衔接
C程序调试过程常见的错误
C语言可以应用在哪些领域?
力扣练习——单词搜索
【STM32】 ADC模数转换