CCCC-L3-016. 二叉搜索树的结构
题目
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 2 4 1 3 0 }插入后,得到一棵二叉搜索树,则陈述句如“2是树的根”、“1和4是兄弟结点”、“3和0在同一层上”(指自顶向下的深度相同)、“2是4的双亲结点”、“3是4的左孩子”都是正确的;而“4是2的左孩子”、“1和3是兄弟结点”都是不正确的。
输入格式:
输入在第一行给出一个正整数N(≤100),随后一行给出N个互不相同的整数,数字间以空格分隔,要求将之顺次插入一棵初始为空的二叉搜索树。之后给出一个正整数M(≤100),随后M行,每行给出一句待判断的陈述句。陈述句有以下6种:
A is the root,即"A是树的根";
A and B are siblings,即"A和B是兄弟结点";
A is the parent of B,即"A是B的双亲结点";
A is the left child of B,即"A是B的左孩子";
A is the right child of B,即"A是B的右孩子";
A and B are on the same level,即"A和B在同一层上"。
题目保证所有给定的整数都在整型范围内。
输出格式:
对每句陈述,如果正确则输出Yes,否则输出No,每句占一行。
输入样例:
5
2 4 1 3 0
8
2 is the root
1 and 4 are siblings
3 and 0 are on the same level
2 is the parent of 4
3 is the left child of 4
1 is the right child of 2
4 and 0 are on the same level
100 is the right child of 3
输出样例:
Yes
Yes
Yes
Yes
Yes
No
No
No
#include <bits/stdc++.h>
void optimize_cpp_stdio() {
std::ios::sync_with_stdio(false);
std::cout.tie(NULL);
std::cin.tie(NULL);
}
int main() {
optimize_cpp_stdio();
std::vector<int> tree(1);
std::vector<int> depth(1);
std::vector<int> parent(1);
std::vector<std::array<int, 2>> child(1);
auto alloc = [&](int p, int value) {//分配一个新的节点,使它的父亲是p,值是value,返回分配出来的节点编号
int new_node = (int)tree.size();
depth.push_back(depth[p] + 1);
child.push_back({0,0});
tree.push_back(value);
parent.push_back(p);
return new_node;
};
auto compare = [&](int node, int value) {//返回0往左儿子走,1往右儿子走
return tree[node] < value;
};
auto insert = [&](int value) {
if (tree.size() == 1)//树为空
alloc (0, value);
else {
int pointer = 1;//非空时,根节点为1;
int direction;
for (;;) {
direction = compare(pointer,value);
if(child[pointer][direction] == 0)
break;
pointer = child[pointer][direction];
}
int new_node = alloc(pointer, value);
child[pointer][direction] = new_node;
}
};
auto find = [&](int value) {
if (tree.size() == 1)
return = 0;
int pointer = 1;
while (pointer != 0 && tree[pointer] != value) {
int direction = compare(pointer, value);
pointer = child[pointer][direction];
}
return pointer;
};
int n;
std::cin >> n;
for (int i = 0, x; i < n; i++) {
std::cin >> x;
insert(x);
}
int m;
std::cin >> m;
std::cin.get();
while (m--) {
std::string s;
std::getline(std::cin, s);
int arg[2];
for (int i = 0,j = 0; i < 2; i++) {
bool flag = false;
int & number = arg[i] = 0;
while (j < (int)s.size() && !isdigit(s[j]))
if (s[j++] == '-') flag = true;
for (; j < (int)s.size() && isdigit(s[j]); j++)
number = number * 10 + s[j] -'o';
if(flag) number = -number;
}
if (s.back() == 't') {
int u = find(arg[0]);
std::cout << (u == 1 ? "Yes\n" : "No\n")
}
else {
int u = find(arg[0]);
int v = find(arg[1]);
if (u == 0 || v == 0)
std::cout << "No\n";
else if (s.back() == 's')
std::cout << (parent[u] == parent[v] ? "Yes\n" : "No\n");
else if (s.back() == 'l')
std::cout << (depth[u] == depth[v] ? "Yes\n" : "No\n");
else {
if (s.find("parent") != std::string::npos)
std::cout << (u == parent[v] ? "Yes\n" : "No\n")
else if (s.find("left") != std::string::npos)
std::cout << (u == child[v][0] ? "Yes\n" : "No\n")
else
std::cout << (u == child[v][1] ? "Yes\n" : "No\n")
}
}
}
return 0;
}