PAT (Advanced Level) 1110. Complete Binary Tree (25)

判断一棵二叉树是否完全二叉树。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

int n,root;
const int maxn=30;
struct Node
{
    int left;
    int right;
    int dep;
} s[maxn];
int flag[maxn];
vector<int>g[maxn];
int Max_dep=0;

void dfs(int x,int dep)
{
    Max_dep=max(dep,Max_dep);
    s[x].dep=dep;
    g[dep].push_back(x);

    if(s[x].left!=-1) dfs(s[x].left,dep+1);
    if(s[x].right!=-1) dfs(s[x].right,dep+1);
}

int main()
{
    memset(flag,0,sizeof flag);
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        char L[5],R[5];
        scanf("%s%s",L,R);
        if(L[0]==‘-‘) s[i].left=-1;
        else
        {
            int num=0;
            for(int k=0; L[k]; k++) num=num*10+L[k]-‘0‘;
            s[i].left=num;
            flag[num]=1;
        }

        if(R[0]==‘-‘) s[i].right=-1;
        else
        {
            int num=0;
            for(int k=0; R[k]; k++) num=num*10+R[k]-‘0‘;
            s[i].right=num;
            flag[num]=1;
        }
    }

    for(int i=0; i<n; i++)
        if(flag[i]==0) root=i;

    dfs(root,0);

    if(Max_dep==0)
    {
        printf("YES %d\n",g[Max_dep][g[Max_dep].size()-1]);
    }
    else
    {
        bool fail=0;
        for(int i=0; i<=Max_dep; i++)
        {
            if(i<Max_dep)
            {
                if(g[i].size()==(int)pow(2.0,i)) {}
                else fail=1;
            }
            else
            {
                for(int j=0; j<g[i].size(); j=j+2)
                {
                    if(j+1<g[i].size()&&j<g[i].size())
                    {
                        if(s[g[i-1][j/2]].left==g[i][j]&&s[g[i-1][j/2]].right==g[i][j+1]) {}
                        else fail=1;
                    }
                    else
                    {
                        if(s[g[i-1][j/2]].left==g[i][j]) {}
                        else fail=1;
                    }
                }
            }
        }

        if(fail==1) printf("NO %d\n",root);
        else printf("YES %d\n",g[Max_dep][g[Max_dep].size()-1]);
    }
    return 0;
}
时间: 2024-12-07 12:07:58

PAT (Advanced Level) 1110. Complete Binary Tree (25)的相关文章

【PAT甲级】1110 Complete Binary Tree (25分)

题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点. trick: 用char输入子结点没有考虑两位数的结点??... stoi(x)可以将x转化为十进制整数 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace

PAT Advanced Level 1064 Complete Binary Search Tree (30)(30 分)

1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of

1110 Complete Binary Tree (25分) 判断一棵二插树是否是完全二叉树

Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree -- and he

PAT Advanced 1102 Invert a Binary Tree (25分)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off. Now it's your turn to prove that YOU CAN invert a binary tree! Input Specif

PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则即不是. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=22; int two

PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<string> #include<stack> #include<map> #include<algorithm> using

PAT甲级——1110 Complete Binary Tree (完全二叉树)

此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For eac

PAT 1110 Complete Binary Tree[比较]

1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤20) which is the total nu

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any