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
用pos记录中序遍历下标, 然后根据后序和中序建树,在建树过程中赋值level和parent。然后再额外判断一个是否是full tree就可以了。树节点中的 l, r 存储的都是中序遍历中Value对于的Index值, root也是中序遍历中根节点的下标。
#include<bits/stdc++.h>
using namespace std;
struct Node {
int l = -1, r = -1, parent, level;
};
int N, M, K;
string s;
vector<int> post, in;
unordered_map<int, int> pos;
vector<Node> Tree;
int bfs(int inL, int inR, int postRoot, int parent, int level) {
if (inL > inR) return -1;
int inRoot = pos[post[postRoot]];
Tree[inRoot].l = bfs(inL, inRoot - 1, postRoot - (inR - inRoot + 1), inRoot, level + 1);
Tree[inRoot].r = bfs(inRoot + 1, inR, postRoot - 1, inRoot, level + 1);
Tree[inRoot].parent = parent;
Tree[inRoot].level = level;
return inRoot;
}
bool judge(int root) {
queue<int> Q;
int flag1 = 0, flag2 = 0;
Q.push(root);
while(!Q.empty()) {
int temp1 = Q.front();
Q.pop();
if (Tree[temp1].l != -1 && Tree[temp1].r == -1) {
flag1 = 1;
break;
}
if (Tree[temp1].l == -1 && Tree[temp1].r != -1) {
flag1 = 1;
break;
}
if (Tree[temp1].l != -1) {
Q.push(Tree[temp1].l);
}
if (Tree[temp1].r != -1) {
Q.push(Tree[temp1].r);
}
}
if (flag1) return false;
else return true;
}
int main() {
scanf("%d", &N);
Tree.resize(N + 1);
post.resize(N + 1);
in.resize(N + 1);
for (int i = 1; i <= N; i++) scanf("%d", &post[i]);
for (int i = 1; i <= N; i++) {
scanf("%d", &in[i]);
pos[in[i]] = i;
}
int root = bfs(1, N, N, -1, 0);
scanf("%d", &K);
getchar();
for (int i = 0; i < K; i++) {
getline(cin, s);
s = " " + s + " ";
int start = 0, a, b;
vector <string> st;
for (int j = 1; j < s.size(); j++) {
if (s[j] == ' ') {
st.push_back(s.substr(start + 1, j - start - 1));
start = j;
}
}
if (st.size() == 4) {
a = stoi(st[0]);
if (a == in[root]) printf("Yes\n");
else printf("No\n");
} else if (st.size() == 5) {
if (st[1] == "and") {
a = stoi(st[0]);
b = stoi(st[2]);
if (Tree[pos[a]].parent == Tree[pos[b]].parent) printf("Yes\n");
else printf("No\n");
} else {
if (judge(root)) printf("Yes\n");
else printf("No\n");
}
} else if (st.size() == 6) {
a = stoi(st[0]);
b = stoi(st[5]);
if (pos[a] == Tree[pos[b]].parent) printf("Yes\n");
else printf("No\n");
} else if (st.size() == 7) {
a = stoi(st[0]);
b = stoi(st[6]);
if (st[3] == "left") {
if (Tree[pos[b]].l == pos[a]) printf("Yes\n");
else printf("No\n");
} else {
if (Tree[pos[b]].r == pos[a]) printf("Yes\n");
else printf("No\n");
}
} else if (st.size() == 8) {
a = stoi(st[0]);
b = stoi(st[2]);
if (Tree[pos[a]].level == Tree[pos[b]].level) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}