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.

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分析:根据后序遍历和中序遍历 输出层序遍历我的做法是利用后序遍历和中序遍历建树 再层序遍历输出利用两个变量来记录我们需要建子树的范围 另一个变量记录每层的根节点

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 #include<vector>
 4 #include<queue>
 5 #include<stack>
 6 #include<algorithm>
 7 using namespace std;
 8 typedef struct BtNode* Bt;
 9 struct BtNode
10 {
11     int Num;
12     Bt RT;
13     Bt LT;
14 };
15 vector<vector<int> > Order(2);
16 Bt BuildTree(Bt T,int i2,int j2,int j1)
17 {
18     if (i2 >= j2)
19         return T;
20     int i = i2;
21     for (; i < j2; i++)
22     {
23         if (Order[1][i] == Order[0][j1 - 1])
24             break;
25     }
26     if (!T)
27     {
28         T = new BtNode();
29         T->LT = NULL;
30         T->RT = NULL;
31         T->Num = Order[1][i];
32     }
33     T->LT =BuildTree(T->LT,i2,i,j1-j2+i);
34     T->RT = BuildTree(T->RT,i+1,j2,j1-1);
35     return T;
36 }
37 int main()
38 {
39     int N;
40     cin >> N;
41     int num;
42     for(int i=0;i<2;i++)
43         for (int j = 0; j < N; j++)
44         {
45             cin >> num;
46             Order[i].push_back(num);
47         }
48     Bt T=NULL;
49     T = BuildTree(T,0,N,N);
50     queue<Bt> Q;
51     Q.push(T);
52     cout << T->Num;
53     while (!Q.empty())
54     {
55         if (Q.front()->LT)
56         {
57             Q.push(Q.front()->LT);
58             cout << " " << Q.front()->LT->Num;
59         }
60         if (Q.front()->RT)
61         {
62             Q.push(Q.front()->RT);
63             cout << " " << Q.front()->RT->Num;
64         }
65         Q.pop();
66     }
67     return 0;
68 }

原文地址:https://www.cnblogs.com/57one/p/11929098.html

时间: 2024-10-11 23:51:36

1020 Tree Traversals (25 分)的相关文章

PAT Advanced 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.

【PAT】1020 Tree Traversals (25)(25 分)

1020 Tree Traversals (25)(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

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.

1020. Tree Traversals (25) PAT甲级真题

之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后续和中序,先推出整个二叉树,再考虑 对二叉树层次遍历. 本题还有一点要注意的时在输出结果的末尾,如果使用了类似 pirntf("%d ",data); 这样的格式是不对的,一定要对末尾进行判断消除最尾端的空格. 首先最核心的部分是通过两次遍历反推回二叉树:这里的思路是,后续遍历的最末尾,一

1020. Tree Traversals (25)

根据两种遍历方式建立二叉树 层遍历二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 le

1020. Tree Traversals (25) -BFS

题目如下: 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:

浙大pat甲级题目---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. Input Specification: Each

PAT (Advanced Level) 1020. Tree Traversals (25)

递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn=40; int a[maxn],b[maxn]; int n,tot; struc

PAT:1020. Tree Traversals (25) AC

#include<stdio.h> #include<stdlib.h> #include<queue> using namespace std; int POST[32]; //存放后序遍历 int IN[32]; //存放中序遍历 int n; //节点数 struct node { int data; node* l; node* r; }; node* creat(int postL,int postR,int inL,int inR) { if(postL&g