二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)
给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 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; struct tree { tree *left, *right, *f; int height,data; }*root; int n,d,k,a,b; map<int,tree *> q; map<int,int>o; tree *creat(int d) { tree *p = new tree(); p->left = NULL; p->right = NULL; p->f = NULL; p->height = 0; p->data = d; q[d] = p; o[d] = 1; return p; } void insert_(tree *root,tree *t) { if(t->data > root->data) { if(root -> right == NULL)root -> right = t,t -> f = root,t-> height = root -> height + 1; else insert_(root -> right,t); } else { if(root -> left == NULL)root -> left = t,t -> f = root,t-> height = root -> height + 1; else insert_(root -> left,t); } } int main() { cin>>n; if(n) { cin>>d; root = creat(d); } for(int i = 1;i < n;i ++) { cin>>d; tree *p = creat(d); insert_(root,p); } cin>>k; for(int i = 0;i < k;i ++) { cin>>a; string op; cin>>op; if(op == "is") { cin>>op>>op; if(op == "root") { if(o[a] && root == q[a])cout<<"Yes"<<endl; else cout<<"No"<<endl; } else if(op == "parent") { cin>>op>>b; if(o[a] && o[b] && q[b] -> f == q[a])cout<<"Yes"<<endl; else cout<<"No"<<endl; } else if(op == "left") { cin>>op>>op>>b; if(o[a] && o[b] && q[b] -> left == q[a])cout<<"Yes"<<endl; else cout<<"No"<<endl; } else { cin>>op>>op>>b; if(o[a] && o[b] && q[b] -> right == q[a])cout<<"Yes"<<endl; else cout<<"No"<<endl; } } else { cin>>b>>op>>op; if(op == "on") { cin>>op>>op>>op; if(o[a] && o[b] && q[a] -> height == q[b] -> height) { cout<<"Yes"<<endl; } else cout<<"No"<<endl; } else { if(o[a] && o[b] && q[a] -> f == q[b] -> f)cout<<"Yes"<<endl; else cout<<"No"<<endl; } } } }
数组建树。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <map> using namespace std; int pos; struct BST { int l,r,f,d,h; BST() { l = r = f = d = h = 0; } }t[101]; map<int,int> mp; void Insert(int h,int no,int d) { if(d < t[no].d) { if(t[no].l) Insert(h + 1,t[no].l,d); else { t[no].l = ++ pos; t[pos].d = d; t[pos].f = no; t[pos].h = h + 1; } } else { if(t[no].r) Insert(h + 1,t[no].r,d); else { t[no].r = ++ pos; t[pos].d = d; t[pos].f = no; t[pos].h = h + 1; } } } int main() { int n,d,m; cin>>n; cin>>d; t[++ pos].d = d; mp[d] = pos; for(int i = 1;i < n;i ++) { cin>>d; Insert(0,1,d); mp[d] = pos; } cin>>m; string s; int a,b; for(int i = 0;i < m;i ++) { cin>>a; cin>>s; bool flag = pos && mp[a] != 0; if(s == "is") { cin>>s>>s; if(s == "root") { flag &= t[1].d == a; } else if(s == "parent") { cin>>s>>b; flag &= mp[b] != 0 && t[t[mp[b]].f].d == a; } else if(s == "left") { cin>>s>>s>>b; flag &= mp[b] != 0 && t[t[mp[b]].l].d == a; } else { cin>>s>>s>>b; flag &= mp[b] != 0 && t[t[mp[b]].r].d == a; } } else { cin>>b>>s>>s; flag &= mp[b] != 0; if(s == "on") { cin>>s>>s>>s; flag &= t[mp[a]].h == t[mp[b]].h; } else { flag &= t[mp[a]].f == t[mp[b]].f; } } puts(flag ? "Yes" : "No"); } }