PAT甲级2019春季7-4 1159.Structure of a Binary Tree (30 分)
算法笔记总目录
关键英语单词解释
7-4 Structure of a Binary Tree (30 分)
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:
A is the root
A and B are siblings
A is the parent of B
A is the left child of B
A is the right child of B
A and B are on the same level
It is a full tree
Note:
- Two nodes are on the same level, means that they have the same depth.
- A full binary tree is a tree in which every node other than the
leaves has two children.
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 103 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
题意
后序和中序建树,并判断
(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<bits/stdc++.h>
using namespace std;
struct node{
int val,level;
node *parent, *lc, *rc;
node(int v, int lev) : val(v), parent(NULL), lc(NULL), rc(NULL), level(lev) { }
};
bool isFull = true;
unordered_map<int, node*> mp;
vector<int> post, in;
node *root = NULL;
node *create(int postL,int postR,int inL,int inR,int level){
if(postL > postR) return NULL;
node *root = new node(post[postR],level++);
int k;
for(k=inL;k<=inR;k++){
if(in[k]==post[postR])break;
}
int numleft = k-inL;
root->lc = create(postL,postL+numleft-1,inL,k-1,level);
root->rc = create(postL+numleft,postR-1,k+1,inR,level);
if(root->lc != NULL) root->lc->parent = root;
if(root->rc != NULL) root->rc->parent = root;
if ((root -> lc == NULL && root -> rc != NULL) || (root -> lc != NULL && root -> rc == NULL)) isFull = false;
mp[post[postR]] = root;
return root;
}
bool judge(string s) {
int u, v;
if (s.find("root") != string::npos) {
sscanf(s.c_str(), "%d is the root", &u);
return u == root -> val;
} else if (s.find("siblings") != string::npos) {
sscanf(s.c_str(), "%d and %d are siblings", &u, &v);
return mp[u] -> parent == mp[v] -> parent;
} else if (s.find("parent") != string::npos) {
sscanf(s.c_str(), "%d is the parent of %d", &u, &v);
return mp[v] -> parent == mp[u];
} else if (s.find("left") != string::npos) {
sscanf(s.c_str(), "%d is the left child of %d", &u, &v);
return mp[v] -> lc == mp[u];
} else if (s.find("right") != string::npos) {
sscanf(s.c_str(), "%d is the right child of %d", &u, &v);
return mp[v] -> rc == mp[u];
} else if (s.find("level") != string::npos) {
sscanf(s.c_str(), "%d and %d are on the same level", &u, &v);
return mp[u] -> level == mp[v] -> level;
} else {
return isFull;
}
}
int main() {
int n, k;
cin >> n;
post.resize(n);
in.resize(n);
for (int i = 0; i < n; i++) cin >> post[i];
for (int i = 0; i < n; i++) cin >> in[i];
root = create(0, n-1, 0, n-1, 1);
cin >> k;
getchar();
for (int i = 0; i < k; i++) {
string temp;
getline(cin, temp);
if (judge(temp)) printf("Yes\n");
else printf("No\n");
}
return 0;
}
还没有评论,来说两句吧...