原题链接:PAT 1159 Structure of a Binary Tree(30分)
参考的浒鱼鱼的博客:PAT甲级 1159 Structure of a Binary Tree(30分)
关键词:dfs建树
注意:full binary tree:满二叉树 complete binary tree:完全二叉树
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.
Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:
Note:
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 no more than 10^3 and are separated by a space.
Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A and B in the statements are in the tree.
Output Specification:
For each statement, print in a line Yes if it is correct, or No if not.
Sample Input:
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Sample Output:
Yes
No
Yes
No
Yes
Yes
Yes
题目大意:给出二叉树的后序和中序序列,再给出几个陈述让你判断是否正确。
浒鱼鱼的分析: 给定后序和中序,这个是PAT甲级题库中很常见的知识点“二叉树的遍历”,可以参考《算法笔记》9.2节和题库的1086、1119、1138。
2.因为可能不是完全二叉树,同时输出的东西也挺复杂的,所以应该果断用DFS建二叉树。建树方法略有不同于1135,是采用map和结构体。
3.本题有7种输出,对应7个数据,都可以在DFS建树中体现:
(1)root,建树的返回就是root的值
(2)siblings,等价于两个节点的祖先是同一个
(3)parent,每个节点存父亲的值
(4)leftchild,A节点的左孩子是B,为了访问A的时候就能读出B节点的值,要用map和结构体
(5)rightchild,B节点的左孩子是A
(6)same level,记录节点的深度
(7)full tree,记录节点有没有左右孩子
代码:
#include
#include
#include
#include
using namespace std;
const int maxn = 40;
int post[maxn], in[maxn]; //后序遍历 中序遍历
struct node{
int l, r, depth, parent;
};
map<int, node> tree; //编号 <-> 结点
bool fullTree = true; //是否是满二叉树
int build(int postr, int inl, int inr, int dpt,int prt){ //post的右端 in的左右端 dpt深度 prt 父节点
if(inl > inr) return -1;
tree[post[postr]].depth = dpt;
tree[post[postr]].parent = prt;
int i = inl;
while(i < inr && in[i] != post[postr]) i++; //在中序遍历中找到根(postr)的位置
//递归左右子树
tree[post[postr]].l = build(postr-inr+i-1, inl, i-1, dpt+1, post[postr]);
tree[post[postr]].r = build(postr-1, i+1, inr, dpt+1, post[postr]);
//如果结点没有左孩子或者右孩子,就不是满二叉树
if(tree[post[postr]].l == -1 && tree[post[postr]].r != -1) fullTree = false;
if(tree[post[postr]].l != -1 && tree[post[postr]].r == -1) fullTree = false;
return post[postr];
}
int main(){
int n, m; //n结点数 m询问数
cin >> n;
for(int i = 0; i < n; i++) scanf("%d",&post[i]);
for(int i = 0; i<n; i++) scanf("%d",&in[i]);
int root = build(n-1, 0, n-1, 0, -1);
scanf("%d\n",&m);
string s1, s2, s3, s4, s5, s6, s7;
for(int i = 0; i < m; i++){
bool flag = false;
cin >> s1 >> s2;
if(s2 == "and"){
cin >> s3 >> s4 >> s5;
if(s5=="siblings"){
if(tree[stoi(s1)].parent == tree[stoi(s3)].parent) flag = true;
}
else{
if(tree[stoi(s1)].depth == tree[stoi(s3)].depth) flag = true;
}
}
else{
cin >> s3 >> s4;
if(s4=="root"){
if(root == stoi(s1)) flag = true;
}
else if(s4 == "parent"){
cin >> s5 >> s6;
if(tree[stoi(s6)].parent == stoi(s1)) flag = true;
}
else if(s4 == "left"){
cin >> s5 >> s6 >> s7;
if(tree[stoi(s7)].r == stoi(s1)) flag = true;
}
else if(s4 == "right"){
cin >> s5 >> s6 >> s7;
if(tree[stoi(s7)].r == stoi(s1)) flag = true;
}else{
if(fullTree) flag = true;
}
}
if(flag) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}