当前位置:网站首页>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);
}
}
边栏推荐
- Does LabVIEW really close the COM port using VISA Close?
- 57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)
- 为什么限制了Oracle的SGA和PGA,OS仍然会用到SWAP?
- nacos安装与配置
- 研究生新同学,牛人看英文文献的经验,值得你收藏
- Pytorch模型训练实用教程学习笔记:三、损失函数汇总
- The solution to the vtk volume rendering code error (the code can run in vtk7, 8, 9), and the VTK dataset website
- Heavy cover special | intercept 99% malicious traffic, reveal WAF offensive and defensive drills best practices
- 即时通讯开发移动端弱网络优化方法总结
- 17. Load balancing
猜你喜欢
随机推荐
17、负载均衡
手撸代码,Redis发布订阅机制实现
【1374. 生成每种字符都是奇数个的字符串】
数据库系统原理与应用教程(072)—— MySQL 练习题:操作题 121-130(十六):综合练习
Pytorch模型训练实用教程学习笔记:一、数据加载和transforms方法总结
openresty 动态黑白名单
MySQL你到底都加了什么锁?
【无标题】
泰德制药董事长郑翔玲荣膺“2022卓越影响力企业家奖” 泰德制药荣获“企业社会责任典范奖”
The graphic details Eureka's caching mechanism/level 3 cache
Pytorch模型训练实用教程学习笔记:二、模型的构建
八百客、销售易、纷享销客各行其道
Pytorch模型训练实用教程学习笔记:三、损失函数汇总
漏刻有时文档系统之XE培训系统二次开发配置手册
58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)
图文详述Eureka的缓存机制/三级缓存
短视频软件开发,Android开发,使用Kotlin实现WebView
密码学的基础:X.690和对应的BER CER DER编码
小数据如何学习?吉大最新《小数据学习》综述,26页pdf涵盖269页文献阐述小数据学习理论、方法与应用
多线程之生产者与消费者