当前位置:网站首页>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;
}
边栏推荐
- Notes on wechat applet development
- Ijkplayer code walk through H264 unpacker application details
- vue3路由缓存组件状态以及设置转场动画
- Construction and verification of Alibaba cloud server webrtc system
- 347. top k high frequency elements heap sort + bucket sort +map
- MFS詳解(七)——MFS客戶端與web監控安裝配置
- Detailed explanation of scrcpy client code walk through H264 raw stream decoding process
- Glide usage notes
- 【sketchup 2021】草图大师中CAD文件的导入与建模(利用cad图纸在草图大师中建立立面模型)、草图大师导出成品为dwg格式的二维、三维、立面效果到cad中打开预览】
- [case] a super easy-to-use tool -- a calculator for programmers
猜你喜欢
Explication détaillée du triangle Yang hui
Vector control of Brushless DC motor (4): sensorless control based on sliding mode observer
欧姆龙平替国产大货—JY-V640半导体晶元盒读写器
MFS详解(七)——MFS客户端与web监控安装配置
MFS details (vii) - - MFS client and Web Monitoring installation configuration
[FAQs for novices on the road] about technology management
SSM framework integration -- > simple background management
JetPack - - - LifeCycle、ViewModel、LiveData
Uniapp secondary encapsulates uview components, and the parent component controls display and hiding
Basic knowledge of knowledge map
随机推荐
Wechat applet (pull-down refresh data) novice to
Custom attribute acquisition of view in applet
Recommend a capacity expansion tool to completely solve the problem of insufficient disk space in Disk C and other disks
十五、IO流(一)
Cross process two-way communication using messenger
JNI exception handling
App performance test: (III) traffic monitoring
Pngquant batch bat and parameter description
Subtotal of constraintlayout
[JS] array de duplication
Kotlin basic string operation, numeric type conversion and standard library functions
Notepad++ settings delete current line shortcut
Notifyitemchanged flash back
App performance test: (II) CPU
【Kernel】驱动编译的两种方式:编译成模块、编译进内核(使用杂项设备驱动模板)
Hidden and wx:if
Unable to locate program input point getrawinputdevicelist in dynamic link library user32 DLL processing
Thread pool learning
Ijkplayer code walkthrough player network video data reading process details
Common websites and tools