当前位置:网站首页>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);
}
}边栏推荐
- 不同的操作加不同的锁详解
- SENSORO成长伙伴计划 x 怀柔黑马科技加速实验室丨以品牌力打造To B企业影响力
- Greenplum数据库源码分析——Standby Master操作工具分析
- 第59章 ApplicationPart内置依赖注入中间件
- Intranet penetration lanproxy deployment
- MySQL开发技巧——存储过程
- 百度无人驾驶商业化已“上路”
- From ordinary advanced to excellent test/development programmer, all the way through
- 工作5年,测试用例都设计不好?来看看大神的用例设计总结
- 10 个 PHP 代码安全漏洞扫描程序
猜你喜欢
随机推荐
明日盛会|ApacheCon Asia 2022 Pulsar 技术议题一览
ssh & scp
为你的“架构”安排定期体检吧!
锐捷交换机基础配置
密码学的基础:X.690和对应的BER CER DER编码
Redis 做网页UV统计
Redis 做签到统计
数据库系统原理与应用教程(072)—— MySQL 练习题:操作题 121-130(十六):综合练习
MySQL开发技巧——并发控制
Gradle系列——Gradle文件操作,Gradle依赖(基于Gradle文档7.5)day3-1
30天刷题计划(五)
【无标题】
二维、三维、四维矩阵每个维度含义解释
XSS range intermediate bypass
第56章 业务逻辑之物流/配送实体定义
regular expression
ARTS_202207W2
MySQL你到底都加了什么锁?
Compse编排微服务实战
【多任务优化】DWA、DTP、Gradnorm(CVPR 2019、ECCV 2018、 ICML 2018)








