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
/*
* @Descripttion:
* @version:
* @Author: iDestro
* @Date: 2019-09-01 08:26:35
* @LastEditors: iDestro
* @LastEditTime: 2019-09-01 14:05:27
*/
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cctype>
#include <queue>
using namespace std;
vector<int> post, in;
struct Node {
int val, height;
Node *parent, *left, *right;
Node(int x, int h) {
this -> val = x;
this -> height = h;
this -> left = this -> right = this -> parent = NULL;
}
};
unordered_map<int, Node*> mp;
bool isfull = true;
// 中序后序建树
Node *create(int postL, int postH, int inL, int inH, int height) {
if (postL > postH) {
return NULL;
}
// 分配节点并赋值高度
Node *root = new Node(post[postH], height);
int k;
for (k = inL; in[k] != post[postH]; k++);
int leftNum = k - inL;
root -> left = create(postL, postL+leftNum-1, inL, inL+leftNum-1, height+1);
root -> right = create(postL+leftNum, postH-1, inL+leftNum+1, inH, height+1);
// 设置孩子节点的父亲指针
if (root -> left != NULL) root -> left -> parent = root;
if (root -> right != NULL) root -> right -> parent = root;
// 判断每个非叶节点是否有两个孩子
if ((root -> left == NULL && root -> right != NULL) || (root -> left != NULL && root -> right == NULL)) {
isfull = false;
}
// 存储<关键值,节点索引>
mp[post[postH]] = root;
return root;
}
Node *root = NULL;
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] -> left == 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] -> right == 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] -> height == mp[v] -> height;
} 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, 0);
cin >> k;
getchar();
for (int i = 0; i < k; i++) {
string temp;
getline(cin, temp);
if (judge(temp)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
本文探讨了如何通过后序遍历和中序遍历序列唯一确定一棵包含不同正整数键值的二叉树,并验证关于该树结构的各种陈述,如节点是否为根、兄弟、父子关系、是否为满二叉树等。

被折叠的 条评论
为什么被折叠?



