PAT--1110. Complete Binary Tree
来源:互联网 发布:修改 linux sftp端口 编辑:程序博客网 时间:2024/05/22 08:15
link to problem
题解
- 主要是构建树,用数组来存储的话很容易判断是不是完全二叉树。
- 找出根节点然后递归构造。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cstdlib>using namespace std;const int maxn = 100;struct node{ int left, right;};int n;int tree[maxn];node a[maxn];int appear[maxn];void build(int root, int t){ if(root == -1) return; tree[t] = root; build(a[root].left, t*2 + 1); build(a[root].right, t*2 + 2);}int main(){#ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin);#endif // ONLINE_JUDGE cin >> n; string s1, s2; for(int i = 0; i < n; ++i){ cin >> s1 >> s2; a[i].left = (s1 == "-") ? -1 : atoi(s1.c_str()); a[i].right = (s2 == "-") ? -1 : atoi(s2.c_str()); if(a[i].left != -1) appear[a[i].left] = true; if(a[i].right != -1) appear[a[i].right] = true; } int root = -1; for(int i = 0; i < n; ++i){ if(!appear[i]){ root = i; break; } } memset(tree, -1, sizeof(tree)); build(root, 0); bool ans = true; for(int i = 0; i < n; ++i){ if(tree[i] == -1){ ans = false; break; } } if(ans) printf("YES %d\n", tree[n - 1]); else printf("NO %d\n", tree[0]); return 0;}
0 0
- PAT--1110. Complete Binary Tree
- 【PAT】1110. Complete Binary Tree
- (PAT)1110. Complete Binary Tree
- PAT-Advanced 1110. Complete Binary Tree (25)
- [pat]1110. Complete Binary Tree (25)
- pat 1110. Complete Binary Tree (25)
- PAT-A: 1110. Complete Binary Tree
- PAT(A) - 1110. Complete Binary Tree (25)
- PAT 1110. Complete Binary Tree (25)
- pat 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25) PAT 甲级
- PAT 甲级 1110. Complete Binary Tree (25)
- PAT 1110 Complete Binary Tree
- solution Of Pat 1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree (25)-PAT甲级真题
- pat甲1110. Complete Binary Tree(完全二叉树)
- 【PAT】【Advanced Level】1110. Complete Binary Tree (25)
- 1110. Complete Binary Tree
- Android自定义SlidingMenu
- [BZOJ1102][POI2007]山峰和山谷Grz(bfs:floodfill)
- mac下的Ipython Notebook-如何修改默认路径
- sping-mvc ajax访问 session超时 添加拦截
- fadeIn跟fadeOut实现动态
- PAT--1110. Complete Binary Tree
- 使用纯C语言通过Direct3D 11的Compute Shader做通用目的计算
- fadeTo使逐渐透明
- 最简单解决c#在UI线程中执行耗时方法导致界面假死的方法
- android java内存与native内存
- Yii框架下的增删改查
- Android Service Framework
- slideup跟slideDown改变高度实现动态
- 如何选择一家公司?