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 hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a -
will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each case, print in one line YES
and the index of the last node if the tree is a complete binary tree, or NO
and the index of the root if not. There must be exactly one space separating the word and the number.
Sample Input 1:
9
7 8
- -
- -
- -
0 1
2 3
4 5
- -
- -
Sample Output 1:
YES 8
Sample Input 2:
8
- -
4 5
0 6
- -
2 3
- 7
- -
- -
Sample Output 2:
NO 1
/*
思路是先建树,找根节点,先序遍历找到最大节点,如果等于n表示完全二叉树
*/
#include<iostream>
#include<cstdlib>
using namespace std;
const int maxn = ; bool isRoot[maxn] = {};
int max_index = -,last_v;
int root = ; struct Node
{
int left,right;
}node[maxn]; int getNum(string s)
{
int iRet = -;
if(s != "-")
{
iRet = atoi(s.c_str());
isRoot[iRet] = ;
}
return iRet;
} void FindRoot(int n)
{
for(int i = ; i < n; i++)
{
if( == isRoot[i])
{
root = i;
break;
}
}
} void preOrder(int v,int index)
{
if(- == v)
{
return;
}
if(index > max_index)
{
max_index = index;
last_v = v;
}
preOrder(node[v].left,index*);
preOrder(node[v].right,index*+);
} int main()
{
int n;
cin >> n;
string s1,s2;
for(int i = ; i < n; i++)
{
cin >> s1 >> s2;
node[i].left = getNum(s1);
node[i].right = getNum(s2);
}
FindRoot(n);
preOrder(root,);
if(max_index == n)
{
printf("YES %d",last_v);
}
else
{
printf("NO %d",root);
}
return ;
}
下面版本三个点段错误,待查
#include<cstdio> const int maxn = ;
struct Node
{
int left,right;
}node[maxn]; int max_index = -,last_v;
bool isRoot[maxn] = {};
int root; int changeNum(char c)
{
int iRet = -;
if(c != '-')
{
iRet = c - '';
isRoot[iRet] = ;
}
return iRet;
} void FindRoot(int n)
{
for(int i = ; i < n; i++)
{
if( == isRoot[i])
{
root = i;
break;
}
}
} void preOrder(int v,int index)
{
if(- == v)
{
return;
}
if(index > max_index)
{
max_index = index;
last_v = v;
}
preOrder(node[v].left,index*);
preOrder(node[v].right,index*+);
} int main()
{
int n;
scanf("%d",&n);
char c1,c2;
for(int i = ; i < n; i++)
{
getchar();
scanf("%c%*c%c",&c1,&c2);
node[i].left = changeNum(c1);
node[i].right = changeNum(c2);
}
FindRoot(n);
preOrder(root,);
if(max_index == n)
{
printf("YES %d",last_v);
}
else
{
printf("NO %d",root);
}
return ;
}
1110 Complete Binary Tree (25 分)的更多相关文章
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)
1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...
- 1110. Complete Binary Tree (25)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)
题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- 1110 Complete Binary Tree
1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...
- 1110 Complete Binary Tree (25 分)
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
随机推荐
- ETL初探
初识ETL 概念 ETL即Extract-Transform-Load.目的是将分散.凌乱.异质的数据整合在一起,为决策提供分析数据,是BI项目(Business Intellifence)项目中重要 ...
- Django进阶(一)
目录 choice参数 MTV与MVC模型 Ajax Ajax传json数据 Ajax传file数据 contentType前后端传输数据编码 ajax + sweetalert 序列化组件 补充 c ...
- Codeforces 1194A. Remove a Progression
传送门 再一次题目看错浪费一小时...退役算了 自己手玩一下发现划掉的都是奇数,最后所有奇数都划掉了,证明也挺显然的 所以直接输出 $2m$ 即可 #include<iostream> # ...
- leecode刷题(24)-- 翻转二叉树
leecode刷题(24)-- 翻转二叉树 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 ...
- Clang编译选项和Pass构建
编译选项相关: 想要添加的选项,以我添加的-fdpu为例子 能通过clang --help得到的选项,整体需要一个解析文件(好像在LLVM项目中都是通过后缀名为xxx.td和xxx.def的文件来进行 ...
- 学习.NET中的AppDomain
学习.NET中的AppDomain 什么是AppDomain?AppDomain是一组程序集的逻辑容器,AppDomain是为了提供隔离而设计的.它提供了保护.配置和终止其中每一个应用程序的隔离 Ap ...
- epoll机制和简述
在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是epoll.相比于select,epoll最大的好处在于它不会随着监听fd数目的 ...
- win7安装xmanager报错error1303、err1317
安装xmanager时出现的一些问题,记录如下. 1.安装xmanager时,提示error1303.如下图,按照百度的办法,创建相应的文件夹后,点击重试. 2.重试后提示err1317,如下图所示. ...
- orace result cache解析
(1) orace 11.2.0.4 在RAC数据库Dataguard切换时,出现少量数据丢失: 解决方案:关闭result cache 功能 或升级数据库版本并安装补丁: ...
- 牛客练习赛46 C 华华跟奕奕玩游戏 (期望,概率)(详解)
链接:https://ac.nowcoder.com/acm/contest/894/C 来源:牛客网 华华跟奕奕玩游戏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...