当前位置:网站首页>105. constructing binary trees from preorder and inorder traversal sequences
105. constructing binary trees from preorder and inorder traversal sequences
2022-06-13 06:40:00 【Mr Gao】
105. Construction of binary trees from traversal sequences of preorder and middle order
Given two arrays of integers preorder and inorder , among preorder Is the prior traversal of a binary tree , inorder Is the middle order traversal of the same tree , Please construct a binary tree and return its root node .
Example 1:
Input : preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output : [3,9,20,null,null,15,7]
Example 2:
Input : preorder = [-1], inorder = [-1]
Output : [-1]
The solution code is as follows :
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;
}
边栏推荐
- MFS详解(七)——MFS客户端与web监控安装配置
- In kotlin?,!,?:,:, - & gt;、== Brief description of symbols
- Session and browser
- 【js】var、let、const
- Cocos released the oppo game prompt "subcontracting failed"
- Time formatting tool ----moment JS (real time display of web page time)
- SSM框架整合--->简单后台管理
- Notepad++ settings delete current line shortcut
- [SketchUp 2021] sketch master's image output and rendering style description [edge setting, plane setting, background setting, watermark setting, modeling setting, sky background creating sky, creatin
- Dynamic link library nesting example
猜你喜欢

端午安康,使用祝福话语生成词云吧

In kotlin?,!,?:,:, - & gt;、== Brief description of symbols

智能文娱稳步发展,景联文科技提供数据采集标注服务

Two uses of bottomsheetbehavior

Construction and verification of Alibaba cloud server webrtc system

景联文科技提供语音数据采集标注服务

SSM framework integration -- > simple background management

MFS explanation (V) -- MFS metadata log server installation and configuration

Dart class inherits and implements mixed operators

机器学习笔记 - 监督学习备忘清单
随机推荐
时间格式化工具----moment.js(网页时间实时展示)
1154. 一年中的第几天
Select all select none JS code implementation
智能金融再升级,景联文科技提供数据采集标注服务
【sketchup 2021】草图大师的图像输出与渲染之样式说明【边线设置、平面设置、背景设置、水印设置、建模设置、天空背景创建天空、利用水印背景创建天空(重要)】
景联文科技:数据采集标注行业现状及解决方案
MFS details (VII) -- MFS client and web monitoring installation configuration
【虚拟机】 VMware虚拟机占用空间过大解决
Custom attribute acquisition of view in applet
package-lock. json
The web server failed to start Port 7001 was already in use
App performance test: (II) CPU
SSM framework integration -- > simple background management
Solution: vscode open file will always overwrite the last opened label
BlockingQueue源码
Failed to extract manifest from apk: processexception:%1 is not a valid Win32 Application.
[JS] array de duplication
[JS] handwriting call(), apply(), bind()
如何使用望友DFM软件进行冷板分析
【2022高考季】作为一个过来人想说的话