题干:
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分开处理。不然就容易误杀。