PAT1020. Tree Traversals

//典型后中省树,这种方法必须有 中序序列来确定根的位置,然后二分建树;

//因为用的vc,之前用序列位置建树通不过,用坐标建树通过了,怀疑vc的功能限制,有时间再来测试,眼下感觉还是坐标好啊,用地址巧了半个晚上了找不着八哥,还不A,哭晕

#include<cstdio>
//#include<cstring>
#include<queue>
#include<vector>
//#include<algorithm>
using namespace std;
const int maxn=31;
struct node
{
int data;
node *lchild,*rchild;
};
int pre[maxn],post[maxn],in[maxn];
int n;
queue<node*>q;
vector<int>level;
node *create(int postl,int postr,int inl,int inr)
{
if(postl>postr)return NULL;
node* root=new node;
root->data=post[postr];
root->lchild=root->rchild=NULL;
int k;
for(k=inl;k<=inr;k++)
if(in[k]==post[postr])break;
int numleft=k-inl;
root->lchild=create(postl,postl+numleft-1,inl,k-1);
root->rchild=create(postl+numleft,postr-1,k+1,inr);
return root;
}
void bfs(node *root)
{
if(root!=NULL)q.push(root);
while(!q.empty())
{
node *tmp=q.front();q.pop();
level.push_back(tmp->data);
if(tmp->lchild!=NULL)q.push(tmp->lchild);
if(tmp->rchild!=NULL)q.push(tmp->rchild);
}
}
int main()
{
freopen("input.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int i;
for( i=0;i<n;i++)scanf("%d",&post[i]);
for( i=0;i<n;i++)scanf("%d",&in[i]);
node *root=create(0,n-1,0,n-1);
bfs(root);
for( i=0;i<level.size();i++)
{
if(i==level.size()-1)printf("%d\n",level[i]);
else printf("%d ",level[i]);
}
}
return 0;
}

时间: 2024-10-06 13:10:20

PAT1020. Tree Traversals的相关文章

pat1020. Tree Traversals (25)

算法思路: 1.后序最后元素为根,根将中序分为左右子树 2.层序遍历利用队列实现,java使用LinkedList import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class Node{ int key; Node left; Node right; public Node(int key){ this.key=key; } } public class Main { static in

Tree Traversals

Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序遍历的特点递归求解,详细内容见代码 #include <iostream> #include <queue> using namespace std; struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; int p

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4205    Accepted Submission(s): 1904 Problem Description A binary tree i

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to

hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)

http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4210    Accepted Submission(s): 1908 Problem Description A binary tree is a

HDU-1701 Binary Tree Traversals

http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3442    Accepted Submission(s): 1541

pat1086. Tree Traversals Again (25)

1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with th

PAT 1020. Tree Traversals (25)

1020. Tree Traversals (25) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

HDU 1710 二叉树的遍历 Binary Tree Traversals

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4330    Accepted Submission(s): 1970 Problem Description A binary tree is a finite set of vertices that is either empty or