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. 四则运算计算器的微信小程序_2 运算

    js文件: function isOperator(value) {   var operatorString = '+-*/()×÷';   return operatorString.indexO ...

  2. 小记---------spark优化之更优分配资源

      spark优化:在一定范围之内,增加资源与性能的提升是成正比的. 因此,       一个cpu core  执行一个task线程. task数: 若有 cpu core 2个.num-execu ...

  3. assign()-JS

    ------------恢复内容开始------------ 现需要在js中,使用一个新的对象的值覆盖旧有对象,js中有一个方法: Object.assign(target,…sources) 当ta ...

  4. mongodb 数据操作(1)

    切换/创建数据库 use test 添加数据db.student.save({name:"J33ack",age:25}) 查看数据库show dbs 删除当前数据库 db.dro ...

  5. Git 一般性操作

    git全局设定 git config --global user.name “码云账号” git config --global user.email “码云注册邮箱” git 定位文件夹cd进入到需 ...

  6. 把两个object对象合并成一个对象 属性名称相同的变为后面对象的值

    object.assign(from,obj)------object.assign(目标对象,被合并的对象)

  7. c++ 初学者的画图库EasyX

    EasyX 什么是easyx? EasyX 是针对 C++ 的图形库,可以帮助 C++语言初学者快速上手图形和游戏编程.其实就是c++的一个图形库让初学者不用只在控制台输出代码,而是在图形界面进行开发 ...

  8. php关于系统环境配置的一些函数

    disk_free_space()  :返回指定目录的可用空间(以字节为单位)

  9. CRM项目讲解和django知识点回顾

    今天想把之前写的CRM项目梳理下,顺便回顾一下djiango的部分重要知识. 1.登录页面(包含简单验证码) 首先来看下CRM的登录页面,样式啥的不重要,大家可以去jquery ui的网站上或者其他地 ...

  10. Reservoir Computing: Harnessing a Universal Dynamical System

    原文连接:https://sinews.siam.org/Details-Page/reservoir-computing-harnessing-a-universal-dynamical-syste ...