当前位置:网站首页>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;
// }
// }
边栏推荐
- 【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
- error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
- 联通网管协议框图
- R语言kaggle 游戏数据探索与可视化
- Map mixed density function and quantile added line
- WSA toolkit installed app store tip doesn't work how to solve?
- Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
- Data Mining-06
- 2.微服务'黑话'集锦及Eureka注册中心相关概念
- GIN a preliminary study, the environment is installed
猜你喜欢
RTSP协议讲解
5G China unicom repeater network management protocol real-time requirements
为什么文字不贴合边
面试攻略系列(四)-- 你不知道的大厂面试
GIN Bind模式获取参数和表单验证
Unicom network management protocol block diagram
现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?
Final assignment of R language data analysis in a university
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
puzzle(016.5)逻辑电路
随机推荐
电脑重装系统还原0x80070005错误如何解决
缓存和数据库一致性问题
CPU-MIPS32 instruction architecture (unlocked pipeline microprocessor)
【瑞吉外卖】day05:增、删、改、查分类以及公共字段自动填充
render解析
Process/Thread Related in Sandbox - 2
5G China unicom AP:B SMS ASCII 转码要求
kustomize入门示例及基本语法使用说明
19、学习MySQL 索引
陈强教授《机器学习及R应用》课程 第十四章作业
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
Q_08 更多信息
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
为什么文字不贴合边
七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
Dry+Bean+Dataset R语言数据分析,报告英文
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
GET POST PUT DELETE request in GIN
GIN a preliminary study, the environment is installed
ARM board adds routing function