L3-1 二叉搜索树的结构 (30 分)

讲解的很不错的链接:https://blog.csdn.net/chudongfang2015/article/details/79446477#commentBox

题目链接:https://pintia.cn/problem-sets/1108203702759940096/problems/1108204121661857797

题目大意:

二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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,即"AB是兄弟结点";
  • A is the parent of B,即"AB的双亲结点";
  • A is the left child of B,即"AB的左孩子";
  • A is the right child of B,即"AB的右孩子";
  • A and B are on the same level,即"AB在同一层上"。

题目保证所有给定的整数都在整型范围内。

模板题:

AC代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 struct node
  4 {
  5     int num;
  6     node *lt,*rt,*fq;
  7 }*tmp;
  8 node *Insert(node *root,int num)
  9 {
 10     if(root==NULL)
 11     {
 12         root=new node;
 13         root->num=num;
 14         root->fq=NULL;
 15         root->lt=NULL;
 16         root->rt=NULL;
 17         return root;
 18     }
 19     else
 20     {
 21         if(num<root->num)
 22         {
 23             root->lt=Insert(root->lt,num);
 24             if(root->lt)
 25                 root->lt->fq=root;
 26         }
 27         else
 28         {
 29             root->rt=Insert(root->rt,num);
 30             if(root->rt)
 31                 root->rt->fq=root;
 32         }
 33     }
 34     return root;
 35 }
 36 void Find(node *root,int num)
 37 {
 38     if(root&&!tmp)
 39     {
 40         if(root->num==num)
 41         {
 42             tmp=root;
 43             return ;
 44         }
 45         Find(root->lt,num);
 46         Find(root->rt,num);
 47     }
 48 }
 49 int h(node *root)
 50 {
 51     int h=0;
 52     if(root==NULL)
 53         return 0;
 54     while(root->fq)
 55     {
 56         h++;
 57         root=root->fq;
 58         if(root==NULL)
 59             break;
 60     }
 61     return h;
 62 }
 63 int main()
 64 {
 65     int n,m,q;
 66     cin>>n;
 67     node *head=NULL;
 68     for(int i=0; i<n; i++)
 69     {
 70         int num;
 71         cin>>num;
 72         head=Insert(head,num);
 73     }
 74     head->fq=NULL;
 75     cin>>q;
 76     string str1,str2,str3,str4,str5,str6;
 77     int t1,t2;
 78     while(q--)
 79     {
 80         tmp=NULL;
 81         cin>>t1;
 82         cin>>str1;
 83         if(str1=="is")
 84         {
 85             cin>>str2;
 86             cin>>str3;
 87             if(str3=="root")
 88             {
 89                 Find(head,t1);
 90                 if(tmp==NULL||tmp->num!=head->num)
 91                     cout<<"No"<<endl;
 92                 else
 93                     cout<<"Yes"<<endl;
 94                 continue;
 95             }
 96             if(str3=="parent")
 97             {
 98                 cin>>str4;
 99                 cin>>t2;
100                 Find(head,t2);
101                 if(tmp==NULL)
102                 {
103                     cout<<"No"<<endl;
104                     continue;
105                 }
106                 if(tmp->fq&&tmp->fq->num==t1)
107                 {
108                     cout<<"Yes"<<endl;
109                 }
110                 else
111                     cout<<"No"<<endl;
112             }
113             if(str3=="left")
114             {
115                 cin>>str4>>str5;
116                 cin>>t2;
117                 Find(head,t2);
118                 if(tmp==NULL)
119                 {
120                     cout<<"No"<<endl;
121                     continue;
122                 }
123                 if(tmp->lt&&tmp->lt->num==t1)
124                 {
125                     cout<<"Yes"<<endl;
126                 }
127                 else
128                     cout<<"No"<<endl;
129             }
130             if(str3=="right")
131             {
132                 cin>>str4>>str5;
133                 cin>>t2;
134                 Find(head,t2);
135                 if(tmp==NULL)
136                 {
137                     cout<<"No"<<endl;
138                     continue;
139                 }
140                 if(tmp->rt&&tmp->rt->num==t1)
141                 {
142                     cout<<"Yes"<<endl;
143                 }
144                 else
145                     cout<<"No"<<endl;
146                 continue;
147             }
148         }
149         else if(str1=="and")
150         {
151             cin>>t2;
152             cin>>str2>>str3;
153             if(str3=="siblings")
154             {
155                 Find(head,t1);
156                 node *tmp1=tmp;
157                 //   cout<<tmp1->num<<endl;
158                 if(tmp==NULL)
159                 {
160                     cout<<"No"<<endl;
161                     continue;
162                 }
163                 tmp=NULL;
164                 Find(head,t2);
165                 node *tmp2=tmp;
166                 if(tmp==NULL)
167                 {
168                     cout<<"No"<<endl;
169                     continue;
170                 }
171                 if(tmp1->fq&&tmp2->fq&&tmp1->fq->num==tmp2->fq->num)
172                 {
173                     cout<<"Yes"<<endl;
174                 }
175                 else
176                     cout<<"No"<<endl;
177                 continue;
178             }
179             if(str3=="on")
180             {
181                 cin>>str4>>str5>>str6;
182                 Find(head,t1);
183                 if(tmp==NULL)
184                 {
185                     cout<<"No"<<endl;
186                     continue;
187                 }
188                 int h1=h(tmp);
189                 //  cout<<h1<<endl;
190                 tmp=NULL;
191                 Find(head,t2);
192                 int h2=h(tmp);
193                 if(tmp==NULL)
194                 {
195                     cout<<"No"<<endl;
196                     continue;
197                 }
198                 if(h1&&h2&&h1==h2)
199                 {
200                     cout<<"Yes"<<endl;
201                 }
202                 else
203                     cout<<"No"<<endl;
204             }
205         }
206     }
207     return 0;
208 }
View Code
原文地址:https://www.cnblogs.com/letlifestop/p/10577355.html