PAT(甲级)2019年春季考试 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 N 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 M M (≤30) is given, followed by M M 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

题意

给出二叉树的中序和后序遍历,判断对于这个数的命题是否正确。

思路

这题比第3题都简单,他们应该换一下顺序……
首先根据中序和后序遍历建树,在建树过程中就可以判断是不是满二叉树,为了便于判断与深度和父节点有关的命题,最好在节点中加入深度信息和父节点的指针。

代码

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

struct node {
    int val, depth;
    node *lChild = nullptr, *rChild = nullptr, *parent = nullptr;
};

int postOrder[35], inOrder[35];
bool isFull = true;

node *create(int postRoot, int inL, int inR, int depth, node *parent) {
    if (inL > inR) return nullptr;

    node *root = new node;
    root->depth = depth;
    root->parent = parent;
    root->val = postOrder[postRoot];

    int inRoot = inL;
    while (inOrder[inRoot] != postOrder[postRoot]) ++inRoot;

    root->lChild = create(postRoot - 1 - inR + inRoot, inL, inRoot - 1, depth + 1, root);
    root->rChild = create(postRoot - 1, inRoot + 1, inR, depth + 1, root);

    if ((root->lChild == nullptr && root->rChild != nullptr) ||
        (root->lChild != nullptr && root->rChild == nullptr))
        isFull = false;

    return root;
}

node *dfs(node *root, int target) {
    if (root == nullptr || root->val == target) return root;
    node *ret = dfs(root->lChild, target);
    if (ret && ret->val == target) return ret;
    return dfs(root->rChild, target);
}

int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    for (int i = 0; i < n; ++i)
        cin >> postOrder[i];
    for (int i = 0; i < n; ++i)
        cin >> inOrder[i];

    node *root = create(n - 1, 0, n - 1, 0, nullptr);

    cin >> n;
    cin.get();
    for (int i = 0; i < n; ++i) {
        bool ok;
        string s;
        int a, b;
        getline(cin, s);

        if (*s.rbegin() == 't') {
            sscanf(s.c_str(), "%d", &a);
            ok = a == root->val;
        } else if (*s.rbegin() == 'e') {
            ok = isFull;
        } else if (*s.rbegin() == 's') {
            sscanf(s.c_str(), "%d and %d", &a, &b);
            node *ra = dfs(root, a);
            node *rb = dfs(root, b);
            ok = ra && rb && ra->parent == rb->parent;
        } else if (*s.rbegin() == 'l') {
            sscanf(s.c_str(), "%d and %d", &a, &b);
            node *ra = dfs(root, a);
            node *rb = dfs(root, b);
            ok = ra && rb && ra->depth == rb->depth;
        } else if (s.find("parent") != string::npos) {
            sscanf(s.c_str(), "%d is the parent of %d", &a, &b);
            node *ra = dfs(root, a);
            ok = ra && ((ra->lChild && ra->lChild->val == b) ||
                        (ra->rChild && ra->rChild->val == b));
        } else if (s.find("left") != string::npos) {
            sscanf(s.c_str(), "%d is the left child of %d", &a, &b);
            node *rb = dfs(root, b);
            ok = rb && rb->lChild && rb->lChild->val == a;
        } else {
            sscanf(s.c_str(), "%d is the right child of %d", &a, &b);
            node *rb = dfs(root, b);
            ok = rb && rb->rChild && rb->rChild->val == a;
        }
        cout << (ok ? "Yes\n" : "No\n");
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值