日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【PAT - 甲级1020】Tree Traversals (25分)(树的遍历,给定中序后序,求层次遍历)

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 7

Sample 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); }

存在問題,葉子節點根本沒賦值,就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); }

存在問題,雖然左或右葉子節點沒有值,但是還是給開了節點,也就是不確定有無值的情況下就++tot來開節點了,導致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分)(树的遍历,给定中序后序,求层次遍历)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。