1159 Structure of a Binary Tree 甲级 xp_xht123

本文介绍了如何根据后序遍历和中序遍历序列重建二叉树,并详细解析了如何判断树结构的七个特性:根节点、兄弟关系、父子关系、左右子关系、同一层级、满二叉树以及是否为全树。通过实例展示了如何使用算法解决这些结构问题。

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

解题思路:首先建树(中序 , 后序)同时计算出每个节点的深度depth,并且存一下每个节点的value值和其节点的一一映射关系(题目保证不会重复,这样不需要每一次判断都要重新遍历整颗树)。

下面都必须建立在这个节点存在的前提下

第一点,求root:建树结束后返回的值就是root

第二点,求siblings:层序遍历,是兄弟就是同根生,一个根的两个子树就是兄弟

第三点,求parent,left child,right child:直接调用映射关系判断就行

第四点,求level:调用每个节点的深度,进行判断就行

第五点,求full tree:只要建树的时候没有只有右子树,没有左子树的情况出现,就是full tree

最后一点:字符串处理,找到关键词就行,在一个字符串中出现上述的五点中的任意一点,用一对映射关系去存其type值就行,每一个串中的数字还需要截取出来,存入vector中,每一次有可能不同,所以需要clear

/*
建树
第一点 找到根
第二点 每个点的深度
*/
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
#include<cstring>
#include<queue>

using namespace std;
const int N = 100;

struct Node
{
    int val;
    Node *right = NULL;
    Node *left = NULL;
};

bool flag = true;
int n;
int post[N] , in[N];
unordered_map<int , int>mp;
unordered_map<string , int>pan_sit;
unordered_map<int , Node*>nod;
int depth[1010] , bro[1010];
/*
A is the root     1
A and B are siblings     2
A is the parent of B     3
A is the left child of B     4 
A is the right child of B     5
A and B are on the same level     6
It is a full tree     7
*/
void init()
{
    pan_sit["root"] = 1;
    pan_sit["siblings"] = 2;
    pan_sit["parent"] = 3;
    pan_sit["left"] = 4;
    pan_sit["right"] = 5;
    pan_sit["level"] = 6;
    pan_sit["full"] = 7;
}

Node* build(int il , int ir , int pl , int pr , int de)
{
    Node* root = new Node;
    int k = mp[post[pr]];
    root -> val = in[k];
    depth[root -> val] = de;
    nod[root -> val] = root;
    if(il < k) root -> left = build(il , k - 1 , pl , pl + k - 1 - il , de + 1);
    if(ir > k) root -> right = build(k + 1 , ir , pr - ir + k , pr - 1 , de + 1);
    if(!root -> left && root -> right) flag = false;
    return root;
}

int k;
vector<int>v;
int pan(string s)
{
    int idx = 0;
    for(int i = 0;i < s.length();i ++)
    {
        int j = i;
        while(j < s.length() && s[j] != ' ') j ++;
        string res = s.substr(i , j - i);
        //cout << res << " ";
        if(res[0] <= '9' && res[0] >= '0') v.push_back(stoi(res));
        if(pan_sit[res]) idx = pan_sit[res];
        i = j;
    }
    return idx;
}

bool bfs(Node *r)
{
    queue<Node*>q;
    q.push(r);
    while(!q.empty())
    {
        Node *t = q.front();
        q.pop();
        
        int cnt = 0;
        if(t -> left) 
        {
            cnt ++;
            q.push(t -> left);
        }
        if(t -> right)
        {
            cnt ++;
            q.push(t -> right);
        }
        if(t -> right && t -> left)
        {
            bro[t -> right -> val] = t -> left -> val;
            bro[t -> left -> val] = t -> right -> val;
        }
    }
}

void judge(int type , Node *r)
{
    if(type == 1) 
    {
        if(v[0] == r -> val) puts("Yes");
        else puts("No");
    }
    else if(type == 2) 
    {
        if(bro[v[0]] == v[1]) puts("Yes");
        else puts("No");
    }
    else if(type == 3)
    {
        if( (!nod[v[0]] -> left) || (!nod[v[0]] -> right))
        {
            puts("No");
            return;
        }
        if(nod[v[0]] -> left -> val == v[1]) puts("Yes");
        else if(nod[v[0]] -> right -> val == v[1]) puts("Yes");
        else puts("No");
    }
    else if(type == 4)
    {
        if(!nod[v[1]] -> left)
        {
            puts("No");
            return;
        }
        if(nod[v[1]] -> left -> val == v[0]) puts("Yes");
        else puts("No");
    }
    else if(type == 5)
    {
        if(!nod[v[1]] -> right)
        {
            puts("No");
            return;
        }
        if(nod[v[1]] -> right -> val == v[0]) puts("Yes");
        else puts("No");
    }
    else if(type == 6)
    {
        if(depth[v[0]] == depth[v[1]]) puts("Yes");
        else puts("No");
    }
    else 
    {
        if(flag) puts("Yes");
        else puts("No");
    }
}


int main()
{
    init();
    cin >> n;
    for(int i = 0;i < n;i ++) cin >> post[i];
    
    for(int i = 0;i < n;i ++)
    {
        cin >> in[i];
        mp[in[i]] = i;
    }
    
    Node* root = build(0 , n - 1 , 0 , n - 1 , 0);
    bfs(root);
    cin >> k;
    getchar();
    while(k --)
    {
        string s;
        getline(cin , s);
        int type = pan(s);
        judge(type , root);
        v.clear();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值