【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 std;
 4 bool vis[1007];
 5 int lchild[27],rchild[27];
 6 int num[27];
 7 int n;
 8 int check(){
 9     memset(vis,0,sizeof(vis));
10     for(int i=0;i<n;++i)
11         vis[num[i]]=1;
12     for(int i=1;i<=n;++i)
13         if(!vis[i])
14             return 0;
15     return 1;
16 }
17 int main(){
18     ios::sync_with_stdio(false);
19     cin.tie(NULL);
20     cout.tie(NULL);
21     cin>>n;
22     memset(lchild,-1,sizeof(lchild));
23     memset(rchild,-1,sizeof(rchild));
24     for(int i=0;i<n;++i){
25         string x,y;
26         cin>>x>>y;
27         if(x!="-")
28             lchild[i]=stoi(x),vis[stoi(x)]=1;
29         if(y!="-")
30             rchild[i]=stoi(y),vis[stoi(y)]=1;
31     }
32     int root=0;
33     for(int i=0;i<n;++i)
34         if(!vis[i])
35             root=i;
36     queue<int>q;
37     q.push(root);
38     int last=0;
39     num[root]=1;
40     while(!q.empty()){
41         int now=q.front();
42         last=now;
43         q.pop();
44         if(lchild[now]!=-1){
45             q.push(lchild[now]);
46             num[lchild[now]]=num[now]*2;
47         }
48         if(rchild[now]!=-1){
49             q.push(rchild[now]);
50             num[rchild[now]]=num[now]*2+1;
51         }
52     }
53     if(check()==1)
54         cout<<"YES "<<last;
55     else
56         cout<<"NO "<<root;
57     return 0;
58 }

原文地址:https://www.cnblogs.com/ldudxy/p/12271542.html

时间: 2024-08-29 22:54:59

【PAT甲级】1110 Complete Binary Tree (25分)的相关文章

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

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甲题题解-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) 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; struc

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 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 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. In

PAT 甲级 1083 List Grades (25 分)

1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. I

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (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 oth