1110. Complete Binary Tree (25)完全二叉树
来源:互联网 发布:网络服务器托管 编辑:程序博客网 时间:2024/06/05 00:14
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<queue>#include<cstring>#include<cstdio>using namespace std;struct node{int num,left,right;};struct node N[100];int n;int isroot[25];int vis[25];int total=1;int str_int(char a[]){ if (a[0]=='-') return -1; else { int sum=0; for (int i=0;i<strlen(a);i++) sum=sum*10+a[i]-'0'; return sum; }}int check(){ for (int i=0;i<n;i++) { if (vis[i]==0) return 0; } return 1;}void bfs(int root){ queue<int> que; que.push(root); int endx=root; while(!que.empty()) { int num=que.front(); que.pop(); if (N[num].left==-1) { if (total==n) { printf("YES %d",endx); return ; } else { printf("NO %d",root); return ; } } else { que.push(N[num].left); endx=N[num].left; total++; } if (N[num].right==-1) { if (total==n) { printf("YES %d",endx); return ; } else { printf("NO %d",root); return ; } } else { que.push(N[num].right); endx=N[num].right; total++; } }}int main(){ int root; char a[3],b[3]; cin>>n; for (int i=0;i<n;i++) { cin>>a>>b; int aa=str_int(a); int bb=str_int(b); isroot[aa]=isroot[bb]=1; N[i].num=i; N[i].left=aa; N[i].right=bb; } for (int i=0;i<n;i++) { if(isroot[i]==0) { root=i; break; } } bfs(root); return 0;}
- 1110. Complete Binary Tree (25)[完全二叉树]
- 1110. Complete Binary Tree (25) <完全二叉树>
- 1110. Complete Binary Tree (25)完全二叉树
- 完全二叉树(Complete Binary Tree)
- 1110. Complete Binary Tree (判断完全二叉树)
- pat甲1110. Complete Binary Tree(完全二叉树)
- 1110. Complete Binary Tree (25) 完全二叉树、树的遍历
- 1110. Complete Binary Tree (25) -- 完全二叉树相关性质, 求树根两种方法
- PAT-1064 Complete Binary Search Tree(完全二叉树)
- 满二叉树(Full Binary Tree)&&完全二叉树(Complete Binary Tree)
- 用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(一)
- 用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(二)
- 04-树6 Complete Binary Search Tree 完全二叉搜索树
- pat(A) 1064. Complete Binary Search Tree(完全二叉树的中序建树)
- pat 甲1064. Complete Binary Search Tree (完全二叉搜索树)
- PAT 1064. Complete Binary Search Tree (30) 快速建立完全二叉树
- 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)
- Android4.4 Uri获取图片及document理解
- iOS用域名或主机名获取IP地址(包含C语言接口 和iOS原生)
- API网关orange在ubuntu安装记录
- HDU 2094 产生冠军
- Cross Compile iptables
- 1110. Complete Binary Tree (25)完全二叉树
- 线程的四种状态以及wait和sleep的区别
- 二叉树1——创建与插入节点
- 设计模式-观察模式
- Xutils创建数据库添加数据
- MVG读书笔记——射影变换的校正(一)
- Linux下双网卡桥接
- hdu6071 2017hdu多校第四场
- Django简单总结