当前位置:网站首页>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;
}
边栏推荐
- AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app
- 【Kernel】驱动编译的两种方式:编译成模块、编译进内核(使用杂项设备驱动模板)
- Usegeneratedkeys=true configuration
- 【js】var、let、const
- Interface oriented programming in C language
- Relationship between fragment lifecycle and activity
- Ijkplayer compilation process record
- El form form verification
- 《MATLAB 神经网络43个案例分析》:第10章 离散Hopfield神经网络的分类——高校科研能力评价
- Array operations in JS
猜你喜欢
Failed to extract manifest from apk: processexception:%1 is not a valid Win32 Application.
MFS details (VII) -- MFS client and web monitoring installation configuration
Basic knowledge of knowledge map
【虚拟机】 VMware虚拟机占用空间过大解决
Free screen recording software captura download and installation
Pngquant batch bat and parameter description
RN Metro packaging process and sentry code monitoring
Relationship between fragment lifecycle and activity
【Kernel】驱动编译的两种方式:编译成模块、编译进内核(使用杂项设备驱动模板)
Dragon Boat Festival wellbeing, use blessing words to generate word cloud
随机推荐
App performance test: (IV) power
Thread pool learning
Applet Use of spaces
Basic knowledge of knowledge map
Analysis of 43 cases of MATLAB neural network: Chapter 10 classification of discrete Hopfield Neural Network -- evaluation of scientific research ability of colleges and Universities
In kotlin?,!,?:,:, - & gt;、== Brief description of symbols
Dart class inherits and implements mixed operators
Recyclerview has data flicker problem using databinding
Analysis of 43 cases of MATLAB neural network: Chapter 11 optimization of continuous Hopfield Neural Network -- optimization calculation of traveling salesman problem
Notepad++ settings delete current line shortcut
Multithreading tests network conditions. Machines in different network segments use nbtstat to judge whether they are powered on
Detailed explanation of scrcpy client code walk through H264 raw stream decoding process
BlockingQueue源码
Common websites and tools
Failed to extract manifest from apk: processexception:%1 is not a valid Win32 Application.
Applet export (use) public function, public data
电镀挂具RFID工序管理解决方案
Logcat -b events and eventlogtags print the location correspondence of the events log in the code
‘ipconfig‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
十六、IO流(二)