当前位置:网站首页>105. 从前序与中序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树
2022-06-13 06:20:00 【Mr Gao】
105. 从前序与中序遍历序列构造二叉树
给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。
示例 1:
输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
输出: [3,9,20,null,null,15,7]
示例 2:
输入: preorder = [-1], inorder = [-1]
输出: [-1]
解题代码如下:
int root_preorder(int *preorder,int left,int right){
return preorder[left];
}
int find_root(int* inorder,int left,int right,int val){
int i;
for(i=left;i<=right;i++){
if(inorder[i]==val){
return i;
}
}
return 0;
}
int find_pre(int* preorder,int preleft,int preright,int* inorder,int inleft,int inright){
int i;
int j;
for(i=preleft;i<=preright;i++){
for(j=inleft;j<=inright;j++){
if(preorder[i]==inorder[j]){
return i;
}
}
}
return 0;
}
void create_tree_dfs(int* preorder,int preleft,int preright,int* inorder,int inleft,int inright,struct TreeNode **r){
if(preleft<=preright){
int val=root_preorder(preorder,preleft,preright);
(*r)->val=val;
(*r)->left=(struct TreeNode* )malloc(sizeof(struct TreeNode));
(*r)->right=(struct TreeNode* )malloc(sizeof(struct TreeNode));
int in_root_index=find_root(inorder,inleft,inright,val);
int left_index=find_pre(preorder,preleft,preright,inorder,inleft,in_root_index-1);
// printf("left_index %d |",left_index);
create_tree_dfs(preorder,left_index,left_index+in_root_index-inleft-1,inorder,inleft,in_root_index-1,&((*r)->left));
int right_index=find_pre(preorder,preleft,preright,inorder,in_root_index+1,inright);
// printf("right_index %d |",right_index);
create_tree_dfs(preorder,right_index,right_index+inright-in_root_index-1,inorder,in_root_index+1,inright,&((*r)->right));
}
else{
*(r)=NULL;
}
}
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){
struct TreeNode* r=(struct TreeNode* )malloc(sizeof(struct TreeNode));
create_tree_dfs(preorder,0,preorderSize-1,inorder,0,inorderSize-1,&r);
return r;
}
边栏推荐
- 【2022高考季】作为一个过来人想说的话
- Kotlin collaboration -- context and exception handling
- El form form verification
- [SketchUp 2021] CAD file import and modeling in the sketch master (establish elevation model in the sketch master by using CAD drawings), and the sketch master exports 2D, 3D and elevation effects of
- [kernel] two methods of driver compilation: compiling into modules and compiling into the kernel (using miscellaneous device driver templates)
- Learning records countless questions (JS)
- Common websites and tools
- Usegeneratedkeys=true configuration
- 端午安康,使用祝福话语生成词云吧
- Hidden and wx:if
猜你喜欢

JetPack - - - DataBinding

Notes on wechat applet development

Two uses of bottomsheetbehavior

《MATLAB 神经网络43个案例分析》:第11章 连续Hopfield神经网络的优化——旅行商问题优化计算

Uniapp secondary encapsulates uview components, and the parent component controls display and hiding

Analyzing server problems using jvisualvm

MFS explanation (VI) -- MFS chunk server installation and configuration

Basic knowledge of knowledge map

El form form verification

Omron Ping replaces the large domestic product jy-v640 semiconductor wafer box reader
随机推荐
Kotlin collaboration - simple use of collaboration
Solutions to common problems in small program development
Kotlin collaboration process +flow download case
Differences among concurrent, parallel, serial, synchronous and asynchronous
Explication détaillée du triangle Yang hui
Pngquant batch bat and parameter description
【js】var、let、const
线程池学习
[SketchUp 2021] CAD file import and modeling in the sketch master (establish elevation model in the sketch master by using CAD drawings), and the sketch master exports 2D, 3D and elevation effects of
JNI exception handling
[2022 college entrance examination season] what I want to say as a passer-by
【新手上路常见问答】一步一步理解程序设计
Time formatting tool ----moment JS (real time display of web page time)
Failed to extract manifest from apk: processexception:%1 is not a valid Win32 Application.
Usegeneratedkeys=true configuration
RFID process management solution for electroplating fixture
Kotlin collaboration -- context and exception handling
Simple use of event bus
Kotlin base generics
Use of smalidea