PAT 甲级 1159 Structure of a Binary Tree

本文详细介绍了如何根据后序遍历和中序遍历序列构建唯一确定的二叉树,并通过广度优先搜索(BFS)预先计算节点间的各种关系,如层次、父节点、左右子节点和兄弟节点,以便高效地验证关于树结构的多种声明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
  • 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 10​3​​ 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;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值