PAT甲级2019春季7-4 1159.Structure of a Binary Tree (30 分)

本文介绍如何根据后序和中序遍历序列构建唯一确定的二叉树,并判断树结构特性,如根节点、兄弟节点、父节点、左右子节点、相同层级及是否为满二叉树。

算法笔记总目录
关键英语单词解释
7-4 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 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

题意

后序和中序建树,并判断
(1)root,建树的返回就是root的值
(2)siblings,等价于两个节点的祖先是同一个
(3)parent,每个节点存父亲的值
(4)leftchild,A节点的左孩子是B,为了访问A的时候就能读出B节点的值,要用map和结构体
(5)rightchild,B节点的左孩子是A
(6)same level,记录节点的深度
(7)full tree,记录节点有没有左右孩子

注:代码仅通过测试样例

#include<bits/stdc++.h>
using namespace std;
struct node{
	int val,level;	
	node *parent, *lc, *rc;
	node(int v, int lev) : val(v), parent(NULL), lc(NULL), rc(NULL), level(lev) {}
};
bool isFull = true;
unordered_map<int, node*> mp;
vector<int> post, in;
node *root = NULL;
node *create(int postL,int postR,int inL,int inR,int level){
	if(postL > postR) return NULL;
	node *root = new node(post[postR],level++);
	int k;
	for(k=inL;k<=inR;k++){
		if(in[k]==post[postR])break;
	}
	int numleft = k-inL;
	root->lc = create(postL,postL+numleft-1,inL,k-1,level);
	root->rc = create(postL+numleft,postR-1,k+1,inR,level);
	if(root->lc != NULL) root->lc->parent = root;
	if(root->rc != NULL) root->rc->parent = root;
	if ((root -> lc == NULL && root -> rc != NULL) || (root -> lc != NULL && root -> rc == NULL))  isFull = false;      
    mp[post[postR]] = root;
	return root;
}

bool judge(string s) {
    int u, v;
    if (s.find("root") != string::npos) {
        sscanf(s.c_str(), "%d is the root", &u);
        return u == root -> val;
    } else if (s.find("siblings") != string::npos) {
        sscanf(s.c_str(), "%d and %d are siblings", &u, &v);
        return mp[u] -> parent == mp[v] -> parent;
    } else if (s.find("parent") != string::npos) {
        sscanf(s.c_str(), "%d is the parent of %d", &u, &v);
        return mp[v] -> parent == mp[u];
    } else if (s.find("left") != string::npos) {
        sscanf(s.c_str(), "%d is the left child of %d", &u, &v);
        return mp[v] -> lc == mp[u];
    } else if (s.find("right") != string::npos) {
        sscanf(s.c_str(), "%d is the right child of %d", &u, &v);
        return mp[v] -> rc == mp[u];
    } else if (s.find("level") != string::npos) {
        sscanf(s.c_str(), "%d and %d are on the same level", &u, &v);
        return mp[u] -> level == mp[v] -> level;
    } else {
        return isFull;
    }
}
int main() {
    int n, k;
    cin >> n;
    post.resize(n);
    in.resize(n);
    for (int i = 0; i < n; i++) cin >> post[i];
    for (int i = 0; i < n; i++) cin >> in[i];
    root = create(0, n-1, 0, n-1, 1);
    cin >> k;
    getchar();
    for (int i = 0; i < k; i++) {
        string temp;
        getline(cin, temp);
        if (judge(temp)) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁de小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值