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

分析:这道题目的建树与以前的通过中序和前序或者中序和后序建树不同,这里给出了每个孩子的左右孩子,根据这些信息来建树。做法是申明一个节点数组,将对应的节点信息填入数组即可,而左右孩子指针就是数组的下标。另外,关于完全二叉树的判断,网上也有很多资料。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; struct Node
{
int data;
int lchild,rchild;
}node[]; int inDreeg[]={}; bool judge(int root,int & last_node)
{
if(root==-) return true;
queue<int> q;
q.push(root);
int flag=;
int now;
while(!q.empty())
{
now=q.front();
q.pop();
//cout<<node[now].data<<endl;
if(flag==)
{
if(node[now].lchild!=-&&node[now].rchild!=-) flag=;
else if(node[now].lchild==-&&node[now].rchild!=-) return false;
else flag=;
}
else
{
if(node[now].lchild!=-||node[now].rchild!=-) return false;
}
if(node[now].lchild!=-) q.push(node[now].lchild);
if(node[now].rchild!=-) q.push(node[now].rchild);
}
last_node=node[now].data;
return true;
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
string l,r;
cin>>l>>r;
node[i].data=i;
if(l=="-")
{
node[i].lchild=-;
}
else
{
int id=atoi(&l[]);
node[i].lchild=id;
inDreeg[id]+=;
} if(r=="-")
{
node[i].rchild=-;
}
else
{
int id=atoi(&r[]);
node[i].rchild=id;
inDreeg[id]+=;
}
}
bool ans;
int root=-;
for(int i=;i<n;i++)
{
if(inDreeg[i]==)
{
root=i;
break;
}
}
int last;
ans=judge(root,last);
if(ans==true)
{
printf("YES %d\n",last);
}
else
{
printf("NO %d\n",root);
}
}

[二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)的更多相关文章

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

  2. 1110. Complete Binary Tree (25)

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

  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甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

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

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

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

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

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

  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. PAT 1110 Complete Binary Tree[判断完全二叉树]

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

随机推荐

  1. 生死系列--WuJie

    WuJie,高中和中专时期的同学,篮球队的队友. 在高三时认识的,我们隔壁班的,但仅限于认识,并未打过交道.高中毕业后考在同一所学校,同一个班,象棋下的很好,喜欢打扑克牌,在班上任团支部书记. 球队时 ...

  2. eclipse各种小图标含义

    参考:https://wenku.baidu.com/view/69a0854df7ec4afe04a1df63.html outline:  实心的代表方法  空心的代表属性  绿色的圆表示公有pu ...

  3. # 20155229 2016-2017-2 《Java程序设计》第七周学习总结

    20155229 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章 lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装 ...

  4. 简单的贝叶斯分类器的python实现

    # -*- coding: utf-8 -*- ''' >>> c = Classy() >>> c.train(['cpu', 'RAM', 'ALU', 'io ...

  5. 2597: [Wc2007]剪刀石头布

    2597: [Wc2007]剪刀石头布 链接 分析: 费用流. 首先转化一下问题,整张图最优的情况是存在$C_n^3$个,即任意3个都行,然后考虑去掉最少不满足的三元环. 如果u赢了v,u向v连一条边 ...

  6. Ceph学习之路(三)Ceph luminous版本部署

    1.配置ceph.repo并安装批量管理工具ceph-deploy [root@ceph-node1 ~]# vim /etc/yum.repos.d/ceph.repo [ceph] name=Ce ...

  7. opencv-Drawing Functions in OpenCV

    1.opencv简单画图形 # coding = utf-8 # 画线.长方形.圆等 import numpy as np import cv2 # 返回一个数组 img = np.zeros((51 ...

  8. Java并发工具类(三):控制并发线程数的Semaphore

    作用 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源. 简介 Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数 ...

  9. 【转】Linux - CentOS 7网络配置

    Linux - CentOS 7网络配置 https://blog.csdn.net/J080624/article/details/78083988   安装完VM后,需要进行网络配置.第一个目标为 ...

  10. Android Library开发注意事项

    Android Library开发注意事项 App Module添加依赖Android Library时可以设置library的优先级, 在编译时,app按照library从低到高的优先级依次与每个l ...