1159 Structure of a Binary Tree (30 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.
Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:
- A is the root
- A and B are siblings
- A is the parent of B
- A is the left child of B
- A is the right child of B
- A and B are on the same level
- It is a full tree
Note:
- Two nodes are on the same level, means that they have the same depth.
- A full binary tree is a tree in which every node other than the leaves has two children.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than 103 and are separated by a space.
Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A
and B
in the statements are in the tree.
Output Specification:
For each statement, print in a line Yes
if it is correct, or No
if not.
Sample Input:
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Sample Output:
Yes
No
Yes
No
Yes
Yes
Yes
经验总结:
emmmm 以前考的所有的关于树的问题以及没有考过的树的问题的大汇总,当然也不算完全的汇总,刚开始没有想到sscanf的时候,真的是有种很绝望的感觉,因此,代码里的查询输入部分很不规整,因为现在没法提交,所以就先这么放出来吧= =。
首先,还是最熟悉的中序加后序建树,这个就很基础了,关键就在建完树之后,虽然这里的查询最多只有30条,我还是使用了一个新结构体,存储所有需要查询的问题的答案,如果每查询一次就进行一次搜索,我觉得可能会超时(当然也没有尝试到底会不会),关键就在BFS了,把parent,lchild,rchild,level,siblings,都存储进来了,这里说一下我为什么初始化为1010,因为他没说是正整数,所以我怕-1也会是数据,而题目又说了所有的数不会大于1000,所以设了个1010来表示空,这里还有个问题,就是siblings,很多人表示不认识,当然我也是猜的,就是兄弟的意思,之前隐约在哪里看过所以有一点点印象,这个如果不知道啥意思,确实就不好AC了。
至于输出,就像我刚开始说的,采用string加上sscanf,效果极佳,用过都说好!感谢sscanf让我看到了AC的曙光!!
这一题我的做法比较基础,当然也可以在利用中序加后序建树的同时,就将左右孩子以及父亲和层数的数据存储好,这样就不用后面进行复杂的BFS了,可以省下很多行代码。。。。
就这么多啦~ 其他问题请参考代码~~
AC代码
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 35;
int n, m, in[maxn], post[maxn];
map<int, int> mp;
bool isfull = true;
struct node
{
int data,level;
node* lchild, *rchild;
};
struct save
{
int lchild, rchild, siblings,level,parent;
save() :lchild(1010), rchild(1010), siblings(1010), parent(1010){}
}save[maxn];
node * create(int inL, int inR, int postL, int postR)
{
if (inL > inR)
return NULL;
int index;
for (index = inL; index <= inR; ++index)
{
if (in[index] == post[postR])
break;
}
node *root = new node();
root->data = post[postR];
root->lchild = create(inL,index-1,postL,postL+index-inL-1);
root->rchild = create(index+1,inR,postL+index-inL,postR-1);
return root;
}
void BFS(node *root)
{
queue<node *> q;
root->level = 1;
q.push(root);
while (q.size())
{
node *x = q.front();
save[mp[x->data]].level = x->level;
q.pop();
if (x->lchild != NULL&&x->rchild == NULL || x->lchild == NULL&&x->rchild != NULL)
{
isfull = false;
}
if (x->lchild != NULL&&x->rchild != NULL)
{
save[mp[x->lchild->data]].siblings = x->rchild->data;
save[mp[x->rchild->data]].siblings = x->lchild->data;
}
if (x->lchild != NULL)
{
save[mp[x->lchild->data]].parent = x->data;
save[mp[x->lchild->data]].level = x->level + 1;
save[mp[x->data]].lchild = x->lchild->data;
x->lchild->level = x->level + 1;
q.push(x->lchild);
}
if (x->rchild != NULL)
{
save[mp[x->rchild->data]].parent = x->data;
save[mp[x->rchild->data]].level = x->level + 1;
save[mp[x->data]].rchild = x->rchild->data;
x->rchild->level = x->level + 1;
q.push(x->rchild);
}
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &post[i]);
for (int i = 0; i < n; ++i)
{
scanf("%d", &in[i]);
mp[in[i]] = i;
}
node *root = create(0, n - 1, 0, n - 1);
scanf("%d", &m);
getchar();
string temp;
BFS(root);
for (int i = 0; i < m; ++i)
{
getline(cin,temp);
if (temp.find("full")!=string::npos)
{
printf("%s\n", isfull ? "Yes" : "No");
}
else if (temp.substr(temp.size() - 11, 11) == "is the root")
{
int x = stoi(temp.substr(0,temp.size()-12));
printf("%s\n", x == post[n - 1] ? "Yes" : "No");
}
else if (temp.substr(temp.size() - 8, 8) == "siblings")
{
int a, b;
sscanf(temp.c_str(),"%d and %d are siblings", &a, &b);
printf("%s\n", save[mp[a]].siblings == b ? "Yes" : "No");
}
else if (temp.find("parent") != string::npos)
{
int a, b;
sscanf(temp.c_str(),"%d is the parent of %d", &a, &b);
printf("%s\n", save[mp[b]].parent == a ? "Yes" : "No");
}
else if (temp.find("left") != string::npos)
{
int a, b;
sscanf(temp.c_str(), "%d is the left child of %d", &a, &b);
printf("%s\n", save[mp[b]].lchild == a ? "Yes" : "No");
}
else if (temp.find("right") != string::npos)
{
int a, b;
sscanf(temp.c_str(), "%d is the right child of %d", &a, &b);
printf("%s\n", save[mp[b]].rchild == a ? "Yes" : "No");
}
else if (temp.find("same") != string::npos)
{
int a, b;
sscanf(temp.c_str(), "%d and %d are on the same level", &a, &b);
printf("%s\n", save[mp[a]].level == save[mp[b]].level ? "Yes" : "No");
}
}
return 0;
}