L3-016 二叉搜索树的结构
按要求判断树的信息,考查对字符串的处理~
#include<bits/stdc++.h> using namespace std; const int maxn=1014; struct node { int data; node * left; node * right; }; void insert (node * &root,int v) { if (root==NULL) { root=new node; root->data=v; root->left=NULL; root->right=NULL; return; } if (v<root->data) insert (root->left,v); else insert (root->right,v); } int isFull=1,maxdepth=-1; unordered_map<int,int> bro,l,r,father,depth,pos; void bfs (node * root) { queue<node *> q; q.push(root); depth[root->data]=0; //maxdepth=max (depth[root->data],maxdepth); while (!q.empty()) { node * now=q.front(); q.pop(); //if (!now->left||!now->right) isFull=0; if ((!now->left&&now->right)||(now->left&&!now->right)) isFull=0; if (now->left&&now->right) { bro[now->left->data]=now->right->data; bro[now->right->data]=now->left->data; } if (now->left) { q.push(now->left); depth[now->left->data]=depth[now->data]+1; //maxdepth=max (depth[now->left->data],maxdepth); l[now->data]=now->left->data; father[now->left->data]=now->data; } if (now->right) { q.push(now->right); depth[now->right->data]=depth[now->data]+1; r[now->data]=now->right->data; father[now->right->data]=now->data; //maxdepth=max (depth[now->right->data],maxdepth); } } } int main () { int N,x; scanf ("%d",&N); node * root=NULL; for (int i=0;i<N;i++) { scanf ("%d",&x); insert (root,x); pos[x]=1; } bfs (root); //if (N<pow(2,maxdepth)-1) isFull=0; int q; scanf ("%d",&q); string s; getchar (); for (int i=0;i<q;i++) { getline (cin,s); //cout<<s.substr(s.length()-1-4,4)<<endl; if (s.substr(s.length()-4,4)=="root") { int rootnum=0; for (int j=0;j<s.length();j++) { if (s[j]>='0'&&s[j]<='9') rootnum=rootnum*10+s[j]-'0'; else break; } if (root->data==rootnum) printf ("Yes\n"); else printf ("No\n"); } else if (s.substr(s.length()-8,8)=="siblings") { int num[2]={0},cnt=0; for (int j=0;j<s.length();j++) { if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0'; else if (s[j-1]>='0'&&s[j-1]<='9') cnt++; } if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n"); else if (bro[num[0]]==num[1]) printf ("Yes\n"); else printf ("No\n"); } else if (s.substr(s.length()-4,4)=="tree") { if (isFull==1) printf ("Yes\n"); else printf ("No\n"); } else if (s.substr(s.length()-5,5)=="level") { int num[2]={0},cnt=0; for (int j=0;j<s.length();j++) { if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0'; else if (s[j-1]>='0'&&s[j-1]<='9') cnt++; } if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n"); else if (depth[num[0]]==depth[num[1]]) printf ("Yes\n"); else printf ("No\n"); } else if (s.find("left")!=string::npos) { int num[2]={0},cnt=0; for (int j=0;j<s.length();j++) { if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0'; else if (s[j-1]>='0'&&s[j-1]<='9') cnt++; } if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n"); else if (l[num[1]]==num[0]) printf ("Yes\n"); else printf ("No\n"); } else if (s.find("right")!=string::npos) { int num[2]={0},cnt=0; for (int j=0;j<s.length();j++) { if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0'; else if (s[j-1]>='0'&&s[j-1]<='9') cnt++; } if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n"); else if (r[num[1]]==num[0]) printf ("Yes\n"); else printf ("No\n"); } else if (s.find("parent")!=string::npos) { int num[2]={0},cnt=0; for (int j=0;j<s.length();j++) { if (s[j]>='0'&&s[j]<='9') num[cnt]=num[cnt]*10+s[j]-'0'; else if (s[j-1]>='0'&&s[j-1]<='9') cnt++; } /*if (pos[num[0]]==0||pos[num[1]]==0) printf ("No\n"); else*/ if (father[num[1]]==num[0]) printf ("Yes\n"); else printf ("No\n"); } } return 0; }