PAT 1159 Structure of a Binary Tree

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 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

思路 :

通过后序中序得到树的结构,然后对树得深度儿子父亲节点信息进行记录,最后判断即可

#include<bits/stdc++.h>
using namespace std;
int post[35];
int in[35];
int lson[1005];
int rson[1005];
int fa[1005];
int dep[1005];
int root;
int n;
string s[35];
int s2i(string ss)
{
	int le = ss.size();
	int sum = 0;
	for (int l = 0; l < le; ++l)
	{
		sum *= 10;
		sum += ss[l] - '0';
	}
	return sum;
}
int work(int l, int r,int x,int d)
{
	if (r < l||x==0)return 0;
	int f = 0;
	for (int i = l; i <= r; ++i)
	{
		if (in[i] == post[x])
		{
			f = i;
			break;
		}
	}

	if (f == 0)return -1;
	dep[post[x]] = d;
	int z = work(l, f - 1, x - 1,d+1);
	if (z <= 0)z = work(l, f - 1, x - 1 - (r - f),d+1);
	lson[post[x]] = z;
	fa[z] = post[x];

	z = work(f + 1, r, x - 1,d+1);
	if (z <= 0)z=work(f + 1, r, x - 1 - (r - f),d+1);
	rson[post[x]] = z;
	fa[z] = post[x];

	return post[x];
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> post[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		cin >> in[i];
	}
	work(1, n, n,1);
	int m; cin >> m;
	getchar();
	root = post[n];
	for (int i = 1; i <= m; ++i)
	{
		getline(cin, s[i]);
		int sz = s[i].size();
		string fg[10];
		int cnt = 0;
		for (int j = 0; j < sz; ++j)
		{
			if (s[i][j] == ' ')
			{
				cnt++;
			}
			else fg[cnt] += s[i][j];
		}
		if (s[i][sz - 1] == 't')
		{
			int sum = s2i(fg[0]);
			if (sum == root)cout << "Yes" << endl;
			else cout << "No" << endl;
		}
		else if (s[i][sz - 1] == 's')
		{
			int sum1 = s2i(fg[0]);
			int sum2 = s2i(fg[2]);
			if (fa[sum1] == fa[sum2])cout << "Yes" << endl;
			else cout << "No" << endl;
		}
		else if (s[i][sz - 1] == 'l')
		{
			int sum1 = s2i(fg[0]);
			int sum2 = s2i(fg[2]);
			if (dep[sum1] == dep[sum2])cout << "Yes" << endl;
			else cout << "No" << endl;
		}
		else if (s[i][sz - 1] == 'e')
		{
			int ff = 0;
			for (int j = 1; j <= n; ++j)
			{
				if (lson[post[j]] > 0 && rson[post[j]] == 0 || lson[post[j]] == 0 && rson[post[j]]> 0)
				{
					ff = 1;
					break;
				}
			}
			if (ff == 0)cout << "Yes" << endl;
			else cout << "No" << endl;
		}
		else
		{
			if (fg[3] == "parent")
			{
				int sum1 = s2i(fg[0]);
				int sum2 = s2i(fg[5]);
				if (fa[sum2] == sum1)cout << "Yes" << endl;
				else cout << "No" << endl;
			}
			else if (fg[3] == "left")
			{
				int sum1 = s2i(fg[0]);
				int sum2 = s2i(fg[6]);
				if (lson[sum2] == sum1)cout << "Yes" << endl;
				else cout << "No" << endl;
			}
			else if (fg[3] == "right")
			{
				int sum1 = s2i(fg[0]);
				int sum2 = s2i(fg[6]);
				if (rson[sum2] == sum1)cout << "Yes" << endl;
				else cout << "No" << endl;
			}
			else cout << "bug" << endl;
		}
	}
	cin >> n;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值