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. scala_类的继承

    Scala继承一个基类跟Java很相似, 但我们需要注意以下几点: 重写一个非抽象方法必须使用override修饰符,以及重写父类属性也必须使用override修饰符. 只有主构造函数才可以往基类的构 ...

  2. Node.js 从入门到茫然系列——入门篇

    在创建服务的时候,我们一般代码就是: var http = require("http"); var server = http.createServer(function(req ...

  3. c++ 方框中绘制菜单代码

    绘制静态菜单 getch与getchar 接收光标控制 一.绘制静态菜单 编写函数void mainmenu( void) 二.getch与getchar getch()的作用是从键盘接收一个字 ...

  4. 4- 算法练习leetcode.com

    0.五大经典算法 动态规划算法----爬楼梯 分治算法-- 贪心算法---零钱问题 回溯算法---迷宫问题 --深度优先 分支限界法 ----广度优先 1.找出下标范围 1.二分法 li = [1,2 ...

  5. c# 抓取和解析网页,并将table数据保存到datatable中(其他格式也可以,自己去修改)

    使用HtmlAgilityPack 基础请参考这篇博客:https://www.cnblogs.com/fishyues/p/10232822.html 下面是根据抓取的页面string 来解析并保存 ...

  6. 一键将 Python2 代码自动转化为 Python3

    问题 Python2 的代码直接在 Python3 环境运行的话会报错误: 如果大量的代码,无论是批量替换,还是逐行修改都够累的,这活儿表示不能干! 有没有办法一键转换呢? 百度了一下发现网上的方法如 ...

  7. java中object数据怎么转换成json数据

    可以通过这个(json-lib-2.3-jdk15.jar)jar里的方法转换 JSONObject json = JSONObject.fromObject(Object); 如果对象数组 JSON ...

  8. javaweb学习5——JSP

    声明:本文只是自学过程中,记录自己不会的知识点的摘要,如果想详细学习JavaWeb,请到孤傲苍狼博客学习,JavaWeb学习点此跳转 本文链接:https://www.cnblogs.com/xdp- ...

  9. 转载Linux下开启MySQL日志

    转载https://blog.csdn.net/weixin_38187469/article/details/79273962 开启mysql日志   1.查看日志是否启用 mysql> sh ...

  10. flask中的宏

    对于flask中的宏编程.我们使用 macro 来对宏起个名称 宏编程 对于我们来说是减少了代码的重用.以及简化了标签的操作,对与开发效率有很大的提升, 在html中.相信大多数都用到了.input ...