二叉树 三种遍历构造二叉树+层次遍历 题目 PAT 树的遍历
2017-03-22 22:29
387 查看
https://www.patest.cn/contests/gplt/L2-006
1.后序遍历+中序遍历,构建二叉树。
2.队列层次输出二叉树
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<string>
using namespace std;
int n;
int in[35],post[35];
struct node
{
int data;
node* lchil;
node* rchil;
}tree[35];
node* buildTree(int *in,int *post,int length)
{
//终止条件
if(length==0)return NULL;
//创建节点,最终返回得到root节点
node *root;
root = new node();
//后序遍历的最后一个节点,为根节点
root->data=*(post+length-1);
//pos中序遍历中找到与后序根节点相同的元素的下标
int pos=0;
while(in[pos]!=post[length-1])
pos++;
//递归建树
//格式(中序序列,后序序列,截断后长度)
root->lchil=buildTree(in,post,pos);
root->rchil=buildTree(in+pos+1,post+pos,length-pos-1);
return root;
}
void show(node *t)
{
queue<node*>q;
q.push(t);
int flag=0;
while(!q.empty())
{
node* tmp=q.front();
q.pop();
if(!flag)
{
cout<<tmp->data;
flag=1;
}
else
cout<<' '<<tmp->data;
if(tmp->lchil!=NULL)
q.push(tmp->lchil);
if(tmp->rchil!=NULL)
q.push(tmp->rchil);
}
cout<<endl;
}
int main()
{
while(cin>>n)
{
for(int i=0;i<n;++i)
cin>>post[i];
for(int i=0;i<n;++i)
cin>>in[i];
//1.
//构造二叉树 保存到root节点
//in是中序序列
//post是后序序列
//n是两种序列长度
node* root=buildTree(in,post,n);
//2.
//遍历bfs获得层次序列
show(root);
}
}
1.后序遍历+中序遍历,构建二叉树。
2.队列层次输出二叉树
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<string>
using namespace std;
int n;
int in[35],post[35];
struct node
{
int data;
node* lchil;
node* rchil;
}tree[35];
node* buildTree(int *in,int *post,int length)
{
//终止条件
if(length==0)return NULL;
//创建节点,最终返回得到root节点
node *root;
root = new node();
//后序遍历的最后一个节点,为根节点
root->data=*(post+length-1);
//pos中序遍历中找到与后序根节点相同的元素的下标
int pos=0;
while(in[pos]!=post[length-1])
pos++;
//递归建树
//格式(中序序列,后序序列,截断后长度)
root->lchil=buildTree(in,post,pos);
root->rchil=buildTree(in+pos+1,post+pos,length-pos-1);
return root;
}
void show(node *t)
{
queue<node*>q;
q.push(t);
int flag=0;
while(!q.empty())
{
node* tmp=q.front();
q.pop();
if(!flag)
{
cout<<tmp->data;
flag=1;
}
else
cout<<' '<<tmp->data;
if(tmp->lchil!=NULL)
q.push(tmp->lchil);
if(tmp->rchil!=NULL)
q.push(tmp->rchil);
}
cout<<endl;
}
int main()
{
while(cin>>n)
{
for(int i=0;i<n;++i)
cin>>post[i];
for(int i=0;i<n;++i)
cin>>in[i];
//1.
//构造二叉树 保存到root节点
//in是中序序列
//post是后序序列
//n是两种序列长度
node* root=buildTree(in,post,n);
//2.
//遍历bfs获得层次序列
show(root);
}
}
//前序遍历 void show(node* t) { if(t!=NULL) { cout<<t->data<<' '; show(t->lchil); show(t->rchil); } } //中序遍历 void show(node* t) { if(t!=NULL) { show(t->lchil); cout<<t->data<<' '; show(t->rchil); } } //后序遍历 void show(node* t) { if(t!=NULL) { show(t->lchil); show(t->rchil); cout<<t->data<<' '; } }
相关文章推荐
- 题目:二叉树的层次遍历 II
- 根据中序和层次遍历序列,构造二叉树
- LeetCode 根据前序和中序遍历构造二叉树的三种解法
- leetcode105 && leetcode106 && PAT 1138. 二叉树的三种遍历的应用
- 二叉树的层次遍历(相当于广度遍历) SJTU OJ 题目 1040二叉树层次遍历
- 层次遍历二叉树-三种不同的方法
- 根据前序和中序便遍历构造完整二叉树,并输出层次遍历
- 二叉链树的创建结点,递归构造二叉树、非递归构造二叉树、递归进行前、中、后遍历,非递归进行前中后遍历、层次遍历
- 数据结构:题目(4)遍历二叉树的四种方法: 依次是:层次、前序、中序、后序
- 由二叉树中序遍历和先序遍历,构造二叉树,经过镜面翻转后输出层次遍历结果
- PAT 1020. Tree Traversals (25)(树的构造与遍历,通过后序中序输出层次遍历)
- 层次遍历二叉树-三种不同的方法
- 二叉树(续) 三种层次遍历算法
- 题目:二叉树的锯齿形层次遍历
- 层次遍历二叉树-三种不同的方法
- 数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
- 层次遍历二叉树-三种不同的方法
- 二叉树的层次,中序非递归遍历,以递归前序的方式构造二叉树,将二叉树中的e更新为d,输出从根结点出发 到指定结点,依次经过的祖先(即路径),由前序和中序还原二叉树
- [置顶] 数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
- 数据结构之二叉树 (构造 拷贝构造 以及前序中序后续三种遍历方法)