PAT_A1020#Tree Traversals

Source:

PAT A1020 Tree Traversals (25 分)

Description:

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.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

Keys:

Code:

 1 /*
 2 time: 2019-06-30 14:40:45
 3 problem: PAT_A1020#Tree Traversals
 4 AC: 08:33
 5
 6 题目大意:
 7 给出后序和中序遍历,打印层序遍历
 8 */
 9 #include<cstdio>
10 #include<queue>
11 using namespace std;
12 const int M=35;
13 int post[M],in[M],n;
14 struct node
15 {
16     int data;
17     node *lchild,*rchild;
18 };
19
20 node *Create(int postL, int postR, int inL, int inR)
21 {
22     if(postL > postR)
23         return NULL;
24     node *root = new node;
25     root->data = post[postR];
26     int k;
27     for(k=inL; k<=inR; k++)
28         if(in[k] == post[postR])
29             break;
30     int numLeft = k-inL;
31     root->lchild = Create(postL,postL+numLeft-1,inL,k-1);
32     root->rchild = Create(postL+numLeft,postR-1,k+1,inR);
33     return root;
34 }
35
36 void LayerOrder(node *root)
37 {
38     queue<node*> q;
39     q.push(root);
40     int pt=0;
41     while(!q.empty())
42     {
43         root = q.front();
44         q.pop();
45         printf("%d%c", root->data, ++pt==n?‘\n‘:‘ ‘);
46         if(root->lchild)
47             q.push(root->lchild);
48         if(root->rchild)
49             q.push(root->rchild);
50     }
51 }
52
53 int main()
54 {
55 #ifdef ONLINE_JUDGE
56 #else
57     freopen("Test.txt", "r", stdin);
58 #endif // ONLINE_JUDGE
59
60     scanf("%d", &n);
61     for(int i=0; i<n; i++)
62         scanf("%d", &post[i]);
63     for(int i=0; i<n; i++)
64         scanf("%d", &in[i]);
65     node *root = Create(0,n-1,0,n-1);
66     LayerOrder(root);
67
68     return 0;
69 }

原文地址:https://www.cnblogs.com/blue-lin/p/11109732.html

时间: 2024-10-20 10:57:49

PAT_A1020#Tree Traversals的相关文章

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

PAT Tree Traversals Again

Tree Traversals Again 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 6) is traversed, the stack operations are: push(1); p