二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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>
using namespace std;
typedef struct node;
typedef node *tree;
struct node
{
int data;
tree L,R;
node(){
L=NULL;
R=NULL;
}
};
void build(tree &bt,int x)
{
if(bt==NULL){
bt=new node();
bt->data=x;
//bt->L=bt->R=NULL;
}
else if(bt->data>x) build(bt->L,x);
else build(bt->R,x);
}
bool isxiongdi(tree bt,int x,int y)//判断是否是兄弟节点
{
if(bt==NULL) return false;
if(bt->L==NULL) return false;//这个必须判断 要不然会段错误
if(bt->R==NULL) return false;
if((bt->L->data==x&&bt->R->data==y)||(bt->R->data==x&&bt->L->data==y)) return true;
if(isxiongdi(bt->L,x,y)){
return true;
}
else return isxiongdi(bt->R,x,y);
}
int level(tree bt,int x,int l)//查找节点是第几层
{
if(bt){
if(bt->data>x) return level(bt->L,x,l+1);
else if(bt->data<x) return level(bt->R,x,l+1);
else return l;
}
}
int n,a[1100];
bool findth(int x)
{
for(int i=0;i<n;i++){
if(a[i]==x) return true;
}
return false;
}
tree aa(tree bt,int a)//找一个节点
{
if(bt){
if(bt->data==a) return bt;
if(a<bt->data) return aa(bt->L,a);
else return aa(bt->R,a);
}
}
bool isparent(tree bt,int a,int b)//判断是否为parent 首先要判断这个节点是否存在 不存在返回false
{
tree t=aa(bt,a);
if((t->L&&t->L->data==b)||(t->R&&t->R->data==b)) return true;
return false;
}
bool isleft(tree bt,int a,int b)
{
tree t=aa(bt,b);
if(t->L&&t->L->data==a) return true;
return false;
}
bool isright(tree bt,int a,int b)
{
tree t=aa(bt,b);
if(t->R&&t->R->data==a) return true;
return false;
}
int main()
{
cin>>n;
tree bt;
bt=NULL;
for(int i=0;i<n;i++){
cin>>a[i];
build(bt,a[i]);
}
int q;
cin>>q;
while(q--){
int a;
cin>>a;
string s;
cin>>s;
if(s=="and"){
int b;
cin>>b;
string t;
getline(cin,t);
int len=t.length();
if(t[len-1]=='s'){
printf("%s\n",isxiongdi(bt,a,b)&&findth(a)&&findth(b)==true?"Yes":"No");
}
else{
//if(level(bt,a,0)==-1||level(bt,b,0)==-1) puts("No");
//else
printf("%s\n",findth(a)&&findth(b)&&(level(bt,a,0)==level(bt,b,0))==true?"Yes":"No");
}
}
else{
cin>>s>>s;
if(s=="root"){
printf("%s\n",findth(a)&&(bt->data==a)==true?"Yes":"No");
}
else{
if(s=="parent"){
cin>>s;
int b;
cin>>b;
printf("%s\n",findth(a)&&findth(b)&&isparent(bt,a,b)==true?"Yes":"No");
}
else if(s=="left"){
cin>>s>>s;
int b;
cin>>b;
printf("%s\n",findth(a)&&findth(b)&&isleft(bt,a,b)==true?"Yes":"No");
}
else{
cin>>s>>s;
int b;
cin>>b;
printf("%s\n",findth(a)&&findth(b)&&isright(bt,a,b)==true?"Yes":"No");
}
}
}
}
return 0;
}