【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 input file contains one test case. For each case, the first line gives a positive integer?N?(≤30), 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 7Sample Output:
4 1 6 3 5 7 2解題報告:
樣例的樹是這樣的:
注意寫好dfs就行了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int n,H[MAX],Z[MAX]; struct Node {int l,r;int val; } R[MAX]; int tot; void dfs(int Zl,int Zr,int Hl,int Hr,int& root) {root = ++tot;int tar = H[Hr],pos,rt = root;if(Zl > Zr) return;R[rt].val = tar;for(int i = Zl; i<=Zr; i++) {if(Z[i] == tar) {pos = i;break;}}R[rt].val = tar;dfs(Zl,pos-1,Hl,Hl+(pos-Zl-1),R[rt].l);dfs(pos+1,Zr,Hl+pos-Zl,Hr-1,R[rt].r); } void bfs() {int flag = 0;queue<int> q;q.push(1);while(q.size()) {int cur = q.front();q.pop();if(flag == 1) printf(" ");flag = 1; printf("%d",R[cur].val);if(R[cur].l != 0 && R[R[cur].l].val != 0) q.push(R[cur].l);if(R[cur].r != 0 && R[R[cur].r].val != 0) q.push(R[cur].r);} } int main() {cin>>n;for(int i = 1; i<=n; i++) cin>>H[i];for(int i = 1; i<=n; i++) cin>>Z[i];int xx;dfs(1,n,1,n,xx);bfs();return 0 ; } /* 71 2 3 4 5 6 72 3 1 5 7 6 4*/?
錯誤代碼1:
剛開始dfs是這么寫的:
void dfs(int Zl,int Zr,int Hl,int Hr,int root) {if(Zl >= Zr) return;int tar = H[Hr],pos,rt = root;R[rt].val = tar;for(int i = Zl; i<=Zr; i++) {if(Z[i] == tar) {pos = i;break;}}R[rt].val = tar;R[rt].l = ++tot; R[rt].r = ++tot;dfs(Zl,pos-1,Hl,Hl+(pos-Zl-1),R[rt].l);dfs(pos+1,Zr,Hl+pos-Zl,Hr-1,R[rt].r); }存在問題,葉子節(jié)點根本沒賦值,就return了。?
后來改成這樣:
void dfs(int Zl,int Zr,int Hl,int Hr,int root) {int tar = H[Hr],pos,rt = root;R[rt].val = tar;if(Zl >= Zr) return;for(int i = Zl; i<=Zr; i++) {if(Z[i] == tar) {pos = i;break;}}R[rt].val = tar;R[rt].l = ++tot; R[rt].r = ++tot;dfs(Zl,pos-1,Hl,Hl+(pos-Zl-1),R[rt].l);dfs(pos+1,Zr,Hl+pos-Zl,Hr-1,R[rt].r); }存在問題,雖然左或右葉子節(jié)點沒有值,但是還是給開了節(jié)點,也就是不確定有無值的情況下就++tot來開節(jié)點了,導致bfs中識別錯誤了。
?其實這樣寫也可以AC
AC代碼2:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int n,H[MAX],Z[MAX]; struct Node {int l,r;int val; } R[MAX]; int tot=1; void dfs(int Zl,int Zr,int Hl,int Hr,int root) {int tar = H[Hr],pos,rt = root;if(Zl > Zr) return;R[rt].val = tar;for(int i = Zl; i<=Zr; i++) {if(Z[i] == tar) {pos = i;break;}}R[rt].val = tar;R[rt].l = ++tot; R[rt].r = ++tot;dfs(Zl,pos-1,Hl,Hl+(pos-Zl-1),R[rt].l);dfs(pos+1,Zr,Hl+pos-Zl,Hr-1,R[rt].r); } void bfs() {int flag = 0;queue<int> q;q.push(1);while(q.size()) {int cur = q.front();q.pop();if(flag == 1) printf(" ");flag = 1; printf("%d",R[cur].val);if(R[cur].l != 0 && R[R[cur].l].val != 0) q.push(R[cur].l);if(R[cur].r != 0 && R[R[cur].r].val != 0) q.push(R[cur].r);} } int main() {cin>>n;for(int i = 1; i<=n; i++) cin>>H[i];for(int i = 1; i<=n; i++) cin>>Z[i];dfs(1,n,1,n,1);bfs();return 0 ; } /* 71 2 3 4 5 6 72 3 1 5 7 6 4*/總之,要讓l>r 和l==r分開處理。不然就容易誤殺。
總結
以上是生活随笔為你收集整理的【PAT - 甲级1020】Tree Traversals (25分)(树的遍历,给定中序后序,求层次遍历)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: druid加密mysql_Druid
- 下一篇: 60Hz屏+6G内存卖3000多元 谷歌
