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的配置文件说明
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 两个exe共享内存数据
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- java.io.tmpdir在哪里?
查找所在目录的方式如下: System.out.println(System.getProperty(“java.io.tmpdir”)); System.getProperty(),还可以获取更多其 ...
- JavaScript 之 原型及原型链
对象[回顾] 通过字面量创建对象 //通过字面量创建对象 var obj1 = { name:'Jack', age: 18 } 通过系统自带的构造函数构造对象 // 通过系统自带的构造函数构造对象 ...
- 分享-QQ/微信/微博(环境搭建)
QQ环境搭建
- @JsonIgnoreProperties 不生效
定义类如下, import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annota ...
- LNMP安装问题
查什么占用了端口 netstat -nlp |grep :80 root@zzx:/usr/local/mysql# netstat -nlp |grep :80tcp 0 ...
- tomcat运行方式详解
tomcat的运行模式有3种 一.bio(blocking I/O) 即阻塞式I/O操作,表示Tomcat使用的是传统的Java I/O操作(即java.io包及其子包).是基于JAVA的HTTP/1 ...
- pyQt 流布局的一个例子
瀑布流布局 from PyQt5.QtCore import QPoint, QRect, QSize, Qt from PyQt5.QtWidgets import (QApplication, Q ...
- canvas实现粒子星空连线
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>离 ...