L3-016 二叉搜索树的结构 (30 分)
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 2 4 1 3 0 }插入后,得到一棵二叉搜索树,则陈述句如“2是树的根”、“1和4是兄弟结点”、“3和0在同一层上”(指自顶向下的深度相同)、“2是4的双亲结点”、“3是4的左孩子”都是正确的;而“4是2的左孩子”、“1和3是兄弟结点”都是不正确的。
输入格式:
输入在第一行给出一个正整数N(≤),随后一行给出N个互不相同的整数,数字间以空格分隔,要求将之顺次插入一棵初始为空的二叉搜索树。之后给出一个正整数M(≤),随后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
题解:主要就是给出来的点可能不在树上,用map标记一下即可,其他的直接模拟即可~~
#include<bits/stdc++.h>
using namespace std;
string str1,str2,str3,str4,str5,str6;
int n,x,y,q;
map<int,int>mp;
struct node
{
node *l,*r;
int data;
};
node *Insert(node *root,int x)
{
if(root==NULL)
{
root=new node;
root->data=x;
root->l=root->r=NULL;
return root;
}
else
{
if(x<root->data)
root->l=Insert(root->l,x);
else if(x>root->data)
root->r=Insert(root->r,x);
}
return root;
} int Find_F(node *root,int x,int y)//x是否是y的父亲
{
if(root)
{
if(root->data==x)
{
if(root->l&&root->l->data==y)
{
return ;
}
if(root->r&&root->r->data==y)
{
return ;
}
}
if(x<root->data)//为了保证结果的正确性,要正确利用二叉搜索的性质进行return,以下同理
return Find_F(root->l,x,y);
if(x>root->data)
return Find_F(root->r,x,y);
}
return ;
}
int Find_L(node *root,int x,int y)//x是否是y的左孩子
{
if(root)
{
if(root->data==y)
{
if(root->l&&root->l->data==x)
{
return ;
}
}
if(y<root->data)
return Find_L(root->l,x,y);
if(y>root->data)
return Find_L(root->r,x,y);
}
return ;
}
int Find_R(node *root,int x,int y)//x是否是y的右孩子
{
if(root)
{
if(root->data==y)
{
if(root->r&&root->r->data==x)
{
return ;
}
}
if(y<root->data)
return Find_R(root->l,x,y);
if(y>root->data)
return Find_R(root->r,x,y);
}
return ;
}
int Find_C(node *root,int x,int y)//x和y是否为兄弟节点
{
if(root)
{
if(root->l&&root->r)
{
if(root->l->data==x&&root->r->data==y)
{
return ;
}
if(root->r->data==x&&root->l->data==y)
{
return ;
}
}
return max(Find_C(root->l,x,y),Find_C(root->r,x,y));//不管左右,只要找到即可
}
return ;
}
int Find_Level(node *root,int x,int step)//x的深度或高度
{
if(root)
{
if(root->data==x)
return step;
if(x<root->data)
return Find_Level(root->l,x,step+);
if(x>root->data)
return Find_Level(root->r,x,step+);
}
return ;
} int main()
{
cin>>n;
node *root=NULL;
for(int i=; i<=n; i++)
{
int r;
cin>>r;
mp[r]++;//将其标记,作为判断条件
root=Insert(root,r);
}
cin>>q;
while(q--)
{
cin>>x;
cin>>str1;
if(str1=="is")
{
cin>>str2>>str3;
if(str3=="root")
{
if(root->data==x)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
else if(str3=="parent")
{
cin>>str4>>y;
int r=Find_F(root,x,y);//x是否是y的father
if(r&&mp[x]&&mp[y])
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
else if(str3=="left")
{
cin>>str4>>str5>>y;
int r=Find_L(root,x,y);//x是否是y的左孩子
if(r&&mp[x]&&mp[y])
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
else if(str3=="right")
{
cin>>str4>>str5>>y;
int r=Find_R(root,x,y);//x是否是y的右孩子
if(r&&mp[x]&&mp[y])
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
else
{
cin>>y>>str2>>str3;//是否为兄弟节点
if(str3=="siblings")
{
int r=Find_C(root,x,y);
if(r&&mp[x]&&mp[y])
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
else
{
cin>>str4>>str5>>str6;//最后一种情况,看是否在同一层上
int Level_x=Find_Level(root,x,);
int Level_y=Find_Level(root,y,);
if((Level_x==Level_y)&&Level_x&&mp[x]&&mp[y])
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
}
return ;
}
L3-016 二叉搜索树的结构 (30 分)的更多相关文章
- 天梯赛练习 L3-016 二叉搜索树的结构 (30分)
题目分析: 用数型结构先建树,一边输入一边建立,根节点的下标为1,所以左孩子为root*2,右孩子为root*2+1,输入的时候可用cin输入字符串也可用scanf不会超时,判断是否在同一层可以判断两 ...
- PTA 7-2 二叉搜索树的结构(30 分)
7-2 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大 ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则 ...
- 二叉搜索树的结构(30 分) PTA 模拟+字符串处理 二叉搜索树的节点插入和非递归遍历
二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根 ...
- 【开200数组解决二叉搜索树的建立、遍历】PAT-L3-016. 二叉搜索树的结构——不用链表来搞定二叉搜索树
L3-016. 二叉搜索树的结构 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它 ...
- L3-1 二叉搜索树的结构 (30 分)
讲解的很不错的链接:https://blog.csdn.net/chudongfang2015/article/details/79446477#commentBox 题目链接:https://pin ...
- L3-016 二叉搜索树的结构 (30 分) 二叉树
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...
- PAT L3-016 二叉搜索树的结构
https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...
- L3-016. 二叉搜索树的结构
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值:它的左.右子树也分别 ...
随机推荐
- Spring框架之一 读取配置文件
以下代码都是来源于官方源码(Spring-4.3.18.RELEASE),此处只是为自己以后深啃先布局出大概流程,请各看官不要浪费时间看 说明: .. 表示省略代码, // 后的如果不是源码自带则为当 ...
- 将list等分成n份
public static <T> Map<Integer, List<T>> spiltList(List<T> list, int num) { M ...
- git 一些操作
1. 代码相关 克隆代码 git clone xxx.git 拉取代码 git pull 查看 修改的 状态 git status 推送代码 git push add 或者 修改代码之后 回滚到 未修 ...
- aliyun服务器lamp配置
1.安装Apache:yum install httpd 2.安装php: yum install php 3.安装mysql客户端:yum install mysql 4.安装mysql服务端:yu ...
- 64位win7+PCL1.6.0+VS2010,64位win10+PCL1.6.0+VS2010
https://blog.csdn.net/liukunrs/article/details/80216329 大体转载自:https://blog.csdn.net/sinat_24206709/a ...
- Lyft、Uber、滴滴涉足汽车租赁领域,能打破既有汽车所有权模式吗?
自共享经济出现之后,众多相关项目遍地开花.这些共享经济项目对于人们来说,最直观的感受就是实惠.性价比高.方便.不过抛开这些使用层面的优点来看的话,共享经济项目最大的特色或许就是改变了事物的所有权.一件 ...
- Vue.js——2.第一个Vue程序
代码 <div id="app"> <p>{{msg}}</p> </div> <script> let vm=new ...
- 移植sqlite
一.参考文档 1.SQLite安装.编译与应用 2.gcc 生成 .a静态库和 .so动态库 二.下载sqlite 1.sqlite官方首页:https://www.sqlite.org/index. ...
- Spring Cloud Alibaba 教程 | Nacos(五)
扩展配置(extended configurations) 通过之前的学习,我们知道应用引入nacos配置中心之后默认将会加载Data ID= ${prefix} - ${spring.profile ...
- liunx搭建配置
预安装准备工具 yum -y install gcc gcc-c++ wget cmake 安装 软件存放目录: /lnmp/ 软件安装目录: /usr/local/ 1.下载安装PCRE wget ...