题意
给定一棵树的后序、中序遍历。然后询问m次,每次判断问句是否正确。询问格式:a是否是根,该树是否是满二叉树,a和b是否是兄弟、是否同层,a是否是b的左孩子、右孩子、双亲。
思路
- 根据给定的后序、中序构造树,造树的同时完成三个哈希表,将结点值与双亲、高度、结点地址建立一一映射关系。
- 层序遍历判断是否是满二叉树,记在flag中。
- 根据映射表能O(1)时间复杂度判断语句是否正确,具体逻辑见题解代码。
题解
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node *l, *r;
}rec[31];
int post[31], in[31];
unordered_map<int, int> high, parent;
unordered_map<int, node*> tmp;
node* mt(int pl, int pr, int il, int ir, int h, int p){
if(il > ir) return NULL;
node * t = (node*)malloc(sizeof(node));
t->data = post[pr];
parent[t->data] = p;
high[t->data] = h;
tmp[t->data] = t;
int i = il;
while(in[i] != post[pr]) i++;
t->l = mt(pl, i - 1 - il + pl, il, i - 1, h + 1, t->data);
t->r = mt(i - il + pl, pr - 1, i + 1, ir, h + 1, t->data);
return t;
}
bool full = true;
void testfull(node * t){
queue<node*> que;
que.push(t);
while(que.size()){
auto tar = que.front();
que.pop();
if(tar->l) que.push(tar->l);
if(tar->r) que.push(tar->r);
if((tar->l && ! tar->r) || (tar->r && ! tar->l)){
full = false;
break;
}
}
}
int main(){
int n; cin>>n;
for(int i = 0; i < n; i ++) cin>>post[i];
for(int i = 0; i < n; i ++) cin>>in[i];
node * t = mt(0, n - 1, 0, n - 1, 1, -1);
testfull(t);
int m; cin>>m;
getchar();
while(m--){
string input; getline(cin, input);
if(input.substr(0, 2) == "It") puts(full ? "Yes" : "No");
else{
int a = stoi(input.substr(0, input.find(' ')));
input = input.erase(0, input.find(' ') + 1);
if(input.substr(0, 3) == "and"){
input.erase(0, input.find(' ') + 1);
int b = stoi(input.substr(0, input.find(' ')));
input.erase(0, input.find(' ') + 1);
input.erase(0, input.find(' ') + 1);
if(input.substr(0, 2) == "on")
puts(high[a] == high[b] ? "Yes" : "No");
else
puts(parent[a] == parent[b] ? "Yes" : "No");
}else{
input.erase(0, 7);
if(input == "root") puts(t->data == a ? "Yes" : "No");
else if(input.substr(0, input.find(' ')) == "parent"){
input.erase(0, input.find(' ') + 1);
input.erase(0, input.find(' ') + 1);
int b = stoi(input);
puts(parent[b] == a ? "Yes" : "No");
}else if(input.substr(0, input.find(' ')) == "left"){
input.erase(0, input.find(' ') + 1);
input.erase(0, input.find(' ') + 1);
input.erase(0, input.find(' ') + 1);
int b = stoi(input);
puts(tmp[b]->l && tmp[b]->l->data == a ? "Yes" : "No");
}else if(input.substr(0, input.find(' ')) == "right"){
input.erase(0, input.find(' ') + 1);
input.erase(0, input.find(' ') + 1);
input.erase(0, input.find(' ') + 1);
int b = stoi(input);
puts(tmp[b]->r && tmp[b]->r->data == a ? "Yes" : "No");
}
}
}
}
return 0;
}
题目
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