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 分)的更多相关文章

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

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

  2. [二叉树建树&完全二叉树判断] 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 ...

  3. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  4. 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 ...

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

    题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...

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

    判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

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

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  8. 1110 Complete Binary Tree

    1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...

  9. 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 ...

随机推荐

  1. python __dict__ 跟 dir()的区别

    __dict__:要是对象的话返回的是一个对象自身的实例属性.不包括类的属性:要是类的__dict__则不包括父类的属性,只包含自身类属性[方法.类变量],不包括实例属性.正是这样.每个实例的实例属性 ...

  2. 从入门到自闭之Python名称空间

    名称空间: 内置空间:python解释器自带的一块空间 全局空间:py文件中顶格写的就是全局空间 局部空间:函数体中就是局部空间 加载顺序: 内置空间 全局空间 局部空间 # def func(): ...

  3. 深入理解hive基础学习

    Hive 是什么?  1.Hive 是基于 Hadoop处理结构化数据的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类 SQL 查询功能. 2.Hive 利用 HDFS 存储数据 ...

  4. FTP服务器上传,下载文件

    public class FtpUtil { /** * * @param host FTP服务器地址 * @param port FTP服务器端口 * @param username FTP登录账号 ...

  5. avascript中实现垃圾桶的功能

    javascript中实现垃圾桶的功能,就像折垃圾桶在拖动目标上触发事件 (源元素):ondragstart - 用户开始拖动元素时触发ondrag - 元素正在拖动时触发ondragend - 用户 ...

  6. 如何查看FQDN

    FQDNFully Qualified Domain Name缩写, 含义完整域名. 例, 台机器主机名(hostname)www, 域缀(domain)example.com, 该主机FQDN应该w ...

  7. 02:Java基础语法(一)

    Java基础语法 Java的关键字及保留字 关键字(Keyword) 关键字的定义和特点定义:被Java语言赋予了特殊含义的单词特点:关键字中所有字母都为小写注意事项:1)true.false.nul ...

  8. 文件I/O简述

    什么是I/O 宏观上讲,I/O是信息处理系统(例如计算机)与外部世界(可能是人或其他信息处理系统)之间的通信.输入(Input)是系统接收的信号或数据,输出(Output)是从其发送的信号或数据.另一 ...

  9. epoll机制和简述

    在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是epoll.相比于select,epoll最大的好处在于它不会随着监听fd数目的 ...

  10. iptables 设置指定IP客户端访问服务器redis端口

    一.需求描述 服务器172.28.18.75开放了6379redis端口,由于没有设置登录密码,所以需要防火墙设置只能指定的IP地址172.28.5.125客户端访问redis端口 二.查看172.2 ...