当前位置:网站首页>JZ7 重建二叉树
JZ7 重建二叉树
2022-08-09 13:02:00 【syc596】
JZ7 重建二叉树
//11
//递归
//pre-根左右
//vin-左根右
import java.util.*;
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
if(pre.length==0||vin.length==0){
return null;
}
TreeNode root=new TreeNode(pre[0]);
for(int i=0;i<vin.length;i++){
if(pre[0]==vin[i]){
root.left=reConstructBinaryTree(Arrays.copyOfRange(pre,1,i+1),
Arrays.copyOfRange(vin,0,i));
root.right=reConstructBinaryTree(Arrays.copyOfRange(pre,i+1,pre.length),
Arrays.copyOfRange(vin,i+1,vin.length));
break;
}
}
return root;
}
}
// //栈
// import java.util.*;
// public class Solution {
// public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
// if(pre.length==0||vin.length==0){
// return null;
// }
// Stack<TreeNode> st=new Stack<>();
// TreeNode root=new TreeNode(pre[0]);
// TreeNode cur=root;
// for(int i=1,j=0;i<pre.length;i++){
// if(cur.val!=vin[j]){
// cur.left=new TreeNode(pre[i]);
// st.push(cur);
// cur=cur.left;
// }else{
// j++;
// while(st.isEmpty()==false&&st.peek().val==vin[j]){
// cur=st.pop();
// j++;
// }
// cur.right=new TreeNode(pre[i]);
// cur=cur.right;
// }
// }
// return root;
// }
// }边栏推荐
- Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
- 周末看点回顾|我国IPv6网络全面建成;2022昇腾AI开发者创享日·南京站成功举办…
- FPGA - Summary of bugs in ISE (in update)
- 昇腾AI开发者创享日南京站!一起CANN机器狗+AI机械臂实现硬核智慧救援!燃爆现场~
- Draw a histogram with plot_hist_numeric()
- gin's middleware and routing grouping
- telnet+ftp to control and upgrade the device
- 【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
- 联通网管协议框图
- Professor Chen Qiang the machine learning and R application course chapter 18 assignments
猜你喜欢

GIN Bind mode to get parameters and form validation

5G China unicom AP:B SMS ASCII Transcoding Requirements

5G China unicom AP:B SMS ASCII 转码要求

error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma

gin's middleware and routing grouping

GIN a preliminary study, the environment is installed

kustomize入门示例及基本语法使用说明

npm install失败

IDEA Gradle 常遇问题(二)(持续更新)

蓝桥杯线上模拟赛——Flex 经典骰子布局
随机推荐
[极客大挑战 2019]Upload
NFS pays special attention to the problem of permissions
The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)
FPGA中串口通信的时钟频率和波特率计数
基于 R 语言的判别分析介绍与实践 LDA和QDA
快来扔鸡蛋。
Deep Learning Based on R Language - Simple Regression Case
Yocto 可以下载的第三方库
ArcEngine(八) 选择要素并高亮显示
JS动画函数封装
FFmpeg multimedia file processing (ffmpeg prints audio and video Meta information)
RTSP协议的实现
【NVIDIA】Tesla V100安装NVIDIA-Driver驱动程序适配CUDA-Toolkit-11.6
行程和用户[阅读理解法]
FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)
Dry+Bean+Dataset R语言数据分析,报告英文
5G 联通网管设计思路
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 13 Assignment
GIN Bind模式获取参数和表单验证
周末看点回顾|我国IPv6网络全面建成;2022昇腾AI开发者创享日·南京站成功举办…