当前位置:网站首页>给定中序遍历和另外一种遍历方法确定一棵二叉树
给定中序遍历和另外一种遍历方法确定一棵二叉树
2022-08-01 19:49:00 【疯疯癫癫才自由】
给定中序遍历和另外一种遍历方法,可以得到一棵二叉树;原因是中序遍历的根节点可以把左右节点分成两半;而其他遍历方法都是给出子树头结点,不能把左右子树分开;因此另外三种遍历方法,任意给出两种遍历方法或者三种遍历方法一起都不能确定一棵二叉树;
1)给出中序遍历和前序遍历确定一棵二叉树:
假定前序遍历区间:[preL,preR];中序遍历:[midL,midR];
k为中序遍历的二叉树的根节点;那么不难得知:
左子树的个数:numl=k-midL;
前序遍历的左子树区间:[preL+1,preL+numl],中序遍历的左子树区间:[midL,midL+numl-1];
前序遍历的右子树区间:[preL+numl+1,preR],中序遍历的右子树区间:[mid+numl+1,midR];
/**
给定中序遍历和另外一种遍历方法,可以得到一棵二叉树;
原因是中序遍历的根节点可以把左右节点分成两半;而其他遍历方法都是给出子树头结点,不能把左右子树
分开;因此另外三种遍历方法,任意给出两种遍历方法或者三种遍历方法一起都不能确定一棵二叉树;
*/
/**
1)给出中序遍历和前序遍历确定一棵二叉树:
假定前序遍历区间:[preL,preR];中序遍历:[midL,midR];
k为中序遍历的二叉树的根节点;那么不难得知:
左子树的个数:numl=k-midL;
前序遍历的左子树区间:[preL+1,preL+numl],中序遍历的左子树区间:[midL,midL+numl-1];
前序遍历的右子树区间:[preL+numl+1,preR],中序遍历的右子树区间:[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 << "输入树的结点数目:\n";
cin >> n;
cout << "输入前序遍历数据\n";
for(int i=0;i<n;++i)
cin >> pre[i];
cout << "输入中序遍历数据:\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)给出中序遍历和后序遍历确定一棵二叉树:
假定后序遍历区间:[behL,behR];中序遍历:[midL,midR];
k为中序遍历的二叉树的根节点;那么不难得知:
右子树的个数:numr=midR-k;
后序遍历的左子树区间:[behL,behR-numr-1],中序遍历的左子树区间:[midL,midR-numr-1];
后序遍历的右子树区间:[behR-numr,behR-1],中序遍历的右子树区间:[midR-numr+1,midR];
/**
2)给出中序遍历和后序遍历确定一棵二叉树:
假定后序遍历区间:[behL,behR];中序遍历:[midL,midR];
k为中序遍历的二叉树的根节点;那么不难得知:
右子树的个数:numr=midR-k;
后序遍历的左子树区间:[behL,behR-numr-1],中序遍历的左子树区间:[midL,midR-numr-1];
后序遍历的右子树区间:[behR-numr,behR-1],中序遍历的右子树区间:[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 << "输入树的结点数目:\n";
cin >> n;
cout << "输入后序遍历数据\n";
for(int i=0;i<n;++i)
cin >> beh[i];
cout << "输入中序遍历数据:\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);
}
}边栏推荐
- What are the application advantages of SaaS management system?How to efficiently improve the digital and intelligent development level of food manufacturing industry?
- How PROE/Croe edits a completed sketch and brings it back to sketching state
- 我的驾照考试笔记(1)
- 用户体验好的Button,在手机上不应该有Hover态
- 数值矩阵的图形表示
- 启明云端分享|盘点ESP8684开发板有哪些功能
- Source code analysis of GZIPOutputStream class
- Gradle系列——Gradle文件操作,Gradle依赖(基于Gradle文档7.5)day3-1
- 有序双向链表的实现。
- 58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)
猜你喜欢
![58: Chapter 5: Develop admin management services: 11: Develop [admin face login, interface]; (not measured) (using Ali AI face recognition) (demonstrated, using RestTemplate to implement interface cal](/img/ab/1c0adeb344329e28010b6ffda5389d.png)
58: Chapter 5: Develop admin management services: 11: Develop [admin face login, interface]; (not measured) (using Ali AI face recognition) (demonstrated, using RestTemplate to implement interface cal

18. Distributed configuration center nacos

第60章 ApplicationPart自动集成整体性和独立性插件项

有点奇怪!访问目的网址,主机能容器却不行

57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)

Greenplum Database Source Code Analysis - Analysis of Standby Master Operation Tools

18、分布式配置中心nacos

Ha ha!A print function, quite good at playing!

Win11怎么安装语音包?Win11语音包安装教程

力扣刷题之移动零
随机推荐
vtk体绘制代码报错的解决办法(代码在vtk7,8,9中都能运行),以及VTK数据集网站
Win10, the middle mouse button cannot zoom in and out in proe/creo
面试突击70:什么是粘包和半包?怎么解决?
启明云端分享|盘点ESP8684开发板有哪些功能
Win11校园网无法连接怎么办?Win11连接不到校园网的解决方法
【webrtc】sigslot : 继承has_slot 及相关流程和逻辑
Pytorch模型训练实用教程学习笔记:一、数据加载和transforms方法总结
Win11怎么安装语音包?Win11语音包安装教程
工作5年,测试用例都设计不好?来看看大神的用例设计总结
小白系统初始化配置资源失败怎么办
密码学的基础:X.690和对应的BER CER DER编码
Arthas 常用命令
Oracle排序某个字段, 如果这个varchar2类型的字段有数字也有文字 , 怎么按照数字大小排序?
Write code anytime, anywhere -- deploy your own cloud development environment based on Code-server
openresty 动态黑白名单
MySQL开发技巧——并发控制
Pytorch模型训练实用教程学习笔记:二、模型的构建
为你的“架构”安排定期体检吧!
easyUI中datagrid中的formatter里面向后台发送请求获取数据
用户体验好的Button,在手机上不应该有Hover态