当前位置:网站首页>剑*offer04 重建二叉树
剑*offer04 重建二叉树
2022-07-23 05:45:00 【wzf6667】
题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
题解
先序遍历就是先输出root节点在左节点再右节点,中序遍历就是先左再root最后右节点
根据先序遍历,我们可以知道这棵树的root节点,即root = pre[0],再去中序遍历中找到root节点,中序遍历中root节点左边的即为左子树,右边的即为右子树。
- 先序 1 2 4 7 3 5 6 8 ->root节点1
中序 4,7,2,1,5,3,8,6
左子树 右子树
所以在先序中可以得到
1 2 4 7 3 5 6 8->2 4 7为左子树,5 6 8为右子树 - 递归 把左右子树分别看成一棵树,root.left = reConstructBinaryTree(pr,ino);
root.right = reConstructBinaryTree(pr,ino);
当然,如果先序中序没有数了,就说明这个root节点的左/右节点为空了。 - 返回root节点。
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root = new TreeNode(pre[0]);
int length = pre.length;
if(length==1){
root.left = null;
root.right = null;
return root;
}
int location=0;
for(int i = 0;i<in.length;i++){
if(root.val == in[i]){
location = i;
}
}
if(location>0){
int[] pr = new int[location];
int[] ino = new int[location];
for(int j =0;j<location;j++){
pr[j]=pre[j+1];
ino[j]=in[j];
}
root.left=reConstructBinaryTree(pr,ino);
}else{
root.left =null;}
if(length-location-1>0){
int[] pr=new int[length-location-1];
int[] ino=new int[length-location-1];
for(int j=location+1;j<length;j++){
ino[j-location-1]=in[j];
pr[j-location-1]=pre[j];
}
root.right=reConstructBinaryTree(pr,ino);
}else{
root.right=null;
}
return root;
}
}
边栏推荐
- (1)ASIO
- Jetson TX1安装 Pytorch
- NLP natural language processing - Introduction to machine learning and natural language processing (I)
- Interpretation of the paper: "bert4bitter: a basic model for improving bitter peptide prediction based on transformer (BERT) bidirectional encoder representation"
- 【存储器了解 RAM flash和eeprom存储器的区别和作用】
- Vscode configuration
- Configure TX1 system + set to solid-state disk startup
- Switch implements expression evaluation
- TeX or LaTeX or MikTeX or TeX Live or CTeX
- 钢结构复习题
猜你喜欢

【AUTOSAR CanDrive 1.学习CanDrive的功能和结构】

B树 和 B+树

Interpretation of the paper: the interpretability of the transformer model of functional genomics

Question bank of basic principles of steel structure

5.4 Pyinstaller库安装与使用

Uni native plug-in development -- Youmeng one click login

【AUTOSAR DCM 1.模块简介(DSL,DSD,DSP)】

Interpretation of the paper: DNA enhancer sequence recognition transformer structure based on Bert and two-dimensional convolutional neural network

匿名上位机v7波形显示

博客搭建五:图床选择
随机推荐
Steel structure review questions
【AUTOSAR CanDrive 2.了解通信Hoh、CanId与PduID的Mapping关系】
基于对象(Object Based)-两个经典类
SCI审稿过程中的几种状态
C语言数据库:详细的说明用学生管理系统了解数据库的操作,简单易懂。
socket基础知识以及各种使用场景
Summary of video coding and decoding related data
5.4 installation and use of pyinstaller Library
Navicat for MySQL 安装教程
嵌入式从入门到精通(入土)——超详细知识点分享1
Interpretation of the paper: DNA enhancer sequence recognition transformer structure based on Bert and two-dimensional convolutional neural network
Knowledge structure of advanced algebra
Simply realize the function of stack
输入三角形边长,求面积
高分子物理考研概念及要点、考点总结
Uni native plug-in development -- Youmeng one click login
[AUTOSAR com 1. introduction to communication protocol stack]
队列与堆的相互实现(纯c实现)
obs插件基础
Axure实现增删改查