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

#include 
using namespace std;
struct TreeNode{
    TreeNode *left, *right, *parent;
    int val;
    TreeNode(int v = 0):left(NULL), right(NULL), parent(NULL), val(v){}
};
int pos[32], ino[32];
map<int, TreeNode*> mp;
map<int, int> lev;
bool isfull = true;

TreeNode *build(int posL, int posR, int inoL, int inoR, int depth){
    if(posL > posR) return NULL;
    int e = pos[posR], idx = inoL;
    while(e != ino[idx]) ++idx;
    TreeNode *r = new TreeNode(e);
    lev[e] = depth;
    r->left = build(posL, posL + idx - 1 - inoL, inoL, idx - 1, depth + 1);
    r->right = build(posR - 1 -(inoR - idx - 1),posR - 1, idx + 1, inoR, depth + 1);
    if(r->left) r->left->parent = r;
    if(r->right) r->right->parent = r;
    if((!r->left && r->right) || (r->left && !r->right)) isfull = false;
    mp[e] = r;
    return r;
}
void sol(string &s, TreeNode *r){
    if(s.find("root") != string::npos){
        int num;
        sscanf(s.c_str(), "%d is the root", &num);
        r->val == num ? printf("Yes\n"):printf("No\n");
    }
    else if(s.find("siblings") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d and %d are siblings", &a, &b);
        TreeNode *p = mp[a], *q = mp[b];
        p->parent == q->parent ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("parent") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d is the parent of %d", &a, &b);
        TreeNode *p = mp[a], *q = mp[b];
        p == q->parent ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("left child") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d is the left child of %d", &a, &b);
        TreeNode *p = mp[a], *q = mp[b];
        p == q->left ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("right child") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d is the right child of %d", &a, &b);
        TreeNode *p = mp[a], *q = mp[b];
        p == q->right ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("level") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d and %d are on the same level", &a, &b);
        lev[a] == lev[b] ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("full") != string::npos){
        isfull ? printf("Yes\n") : printf("No\n");
    }
}
int main()
{
    int n, m;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
        scanf("%d", pos + i);
    }
    for(int i = 0; i < n; ++i){
        scanf("%d", ino + i);
    }
    TreeNode *r = build(0, n - 1, 0, n - 1, 1);
    scanf("%d", &m);
    getchar();
    while(m--){
        string s;
        getline(cin, s);
        sol(s, r);
    }
    return 0;
}

你可能感兴趣的:(PAT(甲级))