当前位置:网站首页>Determine a binary tree given inorder traversal and another traversal method
Determine a binary tree given inorder traversal and another traversal method
2022-08-01 20:00:00 【Madness is free】
Given an inorder traversal and another traversal method,A binary tree can be obtained;The reason is that the root node of an inorder traversal can split the left and right nodes in half;The other traversal methods are to give the subtree head node,The left and right subtrees cannot be separated;Hence the other three traversal methods,Any given two traversal methods or three traversal methods together cannot determine a binary tree;
1)Determine a binary tree given inorder traversal and preorder traversal:
Preorder traversal of the interval is assumed:[preL,preR];中序遍历:[midL,midR];
kis the root node of the binary tree traversed in inorder;Then it is not difficult to know:
左子树的个数:numl=k-midL;
The left subtree interval for preorder traversal:[preL+1,preL+numl],The left subtree interval for inorder traversal:[midL,midL+numl-1];
Right subtree interval for preorder traversal:[preL+numl+1,preR],Right subtree interval for inorder traversal:[mid+numl+1,midR];
/**
Given an inorder traversal and another traversal method,A binary tree can be obtained;
The reason is that the root node of an inorder traversal can split the left and right nodes in half;The other traversal methods are to give the subtree head node,You cannot put left and right subtrees
分开;Hence the other three traversal methods,Any given two traversal methods or three traversal methods together cannot determine a binary tree;
*/
/**
1)Determine a binary tree given inorder traversal and preorder traversal:
Preorder traversal of the interval is assumed:[preL,preR];中序遍历:[midL,midR];
kis the root node of the binary tree traversed in inorder;Then it is not difficult to know:
左子树的个数:numl=k-midL;
The left subtree interval for preorder traversal:[preL+1,preL+numl],The left subtree interval for inorder traversal:[midL,midL+numl-1];
Right subtree interval for preorder traversal:[preL+numl+1,preR],Right subtree interval for inorder traversal:[mid+numl+1,midR];
data:
7
4 1 3 2 6 5 7
1 2 3 4 5 6 7
*/
/**
#include <iostream>
using namespace std;
typedef struct TNode* BinTree;
struct TNode
{
int data;
BinTree lchild,rchild;
TNode()
{
lchild=rchild=NULL;
}
};
BinTree CreateTree_with_Pre_and_Mid(int preL,int preR,int midL,int midR);
void BehTraver(BinTree BT);
const int maxn=20;
int pre[maxn],mid[maxn];
int main()
{
int n;
cout << "Enter the number of nodes in the tree:\n";
cin >> n;
cout << "Enter the preorder traversal data\n";
for(int i=0;i<n;++i)
cin >> pre[i];
cout << "Enter the inorder traversal data:\n";
for(int i=0;i<n;++i)
cin >> mid[i];
BinTree BT=CreateTree_with_Pre_and_Mid(0,n-1,0,n-1);
BehTraver(BT);
return 0;
}
BinTree CreateTree_with_Pre_and_Mid(int preL,int preR,int midL,int midR)
{
if(preL>preR)
return NULL;
BinTree BT=new TNode;
BT->data=pre[preL];
int k;
for(k=midL;k<=midR;++k)
{
if(mid[k]==pre[preL])
break;
}
int numl=k-midL;
BT->lchild=CreateTree_with_Pre_and_Mid(preL+1,preL+numl,midL,midL+numl-1);
BT->rchild=CreateTree_with_Pre_and_Mid(preL+numl+1,preR,midL+numl+1,midR);
return BT;
}
void BehTraver(BinTree BT)
{
if(BT)
{
BehTraver(BT->lchild);
BehTraver(BT->rchild);
printf("%d ",BT->data);
}
}
*/2)Determine a binary tree given in-order traversal and post-order traversal:
A postorder traversal of the interval is assumed:[behL,behR];中序遍历:[midL,midR];
kis the root node of the binary tree traversed in inorder;Then it is not difficult to know:
右子树的个数:numr=midR-k;
The left subtree interval for postorder traversal:[behL,behR-numr-1],The left subtree interval for inorder traversal:[midL,midR-numr-1];
Right subtree interval for postorder traversal:[behR-numr,behR-1],Right subtree interval for inorder traversal:[midR-numr+1,midR];
/**
2)Determine a binary tree given in-order traversal and post-order traversal:
A postorder traversal of the interval is assumed:[behL,behR];中序遍历:[midL,midR];
kis the root node of the binary tree traversed in inorder;Then it is not difficult to know:
右子树的个数:numr=midR-k;
The left subtree interval for postorder traversal:[behL,behR-numr-1],The left subtree interval for inorder traversal:[midL,midR-numr-1];
Right subtree interval for postorder traversal:[behR-numr,behR-1],Right subtree interval for inorder traversal:[midR-numr+1,midR];
data:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
*/
#include <iostream>
using namespace std;
typedef struct TNode* BinTree;
struct TNode
{
int data;
BinTree lchild,rchild;
TNode()
{
lchild=rchild=NULL;
}
};
BinTree CreateTree_with_Beh_and_Mid(int behL,int behR,int midL,int midR);
void PreTraver(BinTree BT);
const int maxn=20;
int beh[maxn],mid[maxn];
int main()
{
int n;
cout << "Enter the number of nodes in the tree:\n";
cin >> n;
cout << "输入后序遍历数据\n";
for(int i=0;i<n;++i)
cin >> beh[i];
cout << "Enter the inorder traversal data:\n";
for(int i=0;i<n;++i)
cin >> mid[i];
BinTree BT=CreateTree_with_Beh_and_Mid(0,n-1,0,n-1);
PreTraver(BT);
return 0;
}
BinTree CreateTree_with_Beh_and_Mid(int behL,int behR,int midL,int midR)
{
if(behL>behR)
return NULL;
BinTree BT=new TNode;
BT->data=beh[behR];
int k;
for(k=midL;k<=midR;++k)
{
if(mid[k]==beh[behR])
break;
}
int numr=midR-k;
BT->lchild=CreateTree_with_Beh_and_Mid(behL,behR-numr-1,midL,midR-numr-1);
BT->rchild=CreateTree_with_Beh_and_Mid(behR-numr,behR-1,midR-numr+1,midR);
return BT;
}
void PreTraver(BinTree BT)
{
if(BT)
{
printf("%d ",BT->data);
PreTraver(BT->lchild);
PreTraver(BT->rchild);
}
}边栏推荐
- 泰德制药董事长郑翔玲荣膺“2022卓越影响力企业家奖” 泰德制药荣获“企业社会责任典范奖”
- 【torch】张量乘法:matmul,einsum
- Pytorch模型训练实用教程学习笔记:三、损失函数汇总
- Acrel-5010重点用能单位能耗在线监测系统在湖南三立集团的应用
- When installing the GBase 8c database, the error message "Resource: gbase8c already in use" is displayed. How to deal with this?
- datax - 艰难debug路
- 漏刻有时文档系统之XE培训系统二次开发配置手册
- 【无标题】
- 部署zabbix
- Redis 做签到统计
猜你喜欢

LTE时域、频域资源

Risc-v Process Attack

How PROE/Croe edits a completed sketch and brings it back to sketching state

【kali-信息收集】(1.3)探测网络范围:DMitry(域名查询工具)、Scapy(跟踪路由工具)

Creo5.0草绘如何绘制正六边形

30天刷题计划(五)

Acrel-5010重点用能单位能耗在线监测系统在湖南三立集团的应用

58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)

安全作业7.25

如何记录分析你的炼丹流程—可视化神器Wandb使用笔记【1】
随机推荐
The solution to the vtk volume rendering code error (the code can run in vtk7, 8, 9), and the VTK dataset website
GEE(8):使用MODIS填补由去云后的Landsat影像计算得到的NDVI数据
【无标题】
The graphic details Eureka's caching mechanism/level 3 cache
nacos安装与配置
30-day question brushing plan (5)
SQL的 ISNULL 函数
CMake教程——Leeds_Garden
KDD2022 | 自监督超图Transformer推荐系统
我的驾照考试笔记(2)
我的驾照考试笔记(1)
Win10, the middle mouse button cannot zoom in and out in proe/creo
1个小时!从零制作一个! AI图片识别WEB应用!
【软考软件评测师】基于规则说明的测试技术下篇
Risc-v Process Attack
MySQL你到底都加了什么锁?
第56章 业务逻辑之物流/配送实体定义
八百客、销售易、纷享销客各行其道
Debug一个ECC的ODP数据源
部署zabbix