PAT (Advanced Level) Practise 1110 Complete Binary Tree (25)
来源:互联网 发布:用淘宝联盟被发现了 编辑:程序博客网 时间:2024/06/05 19:17
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:97 8- -- -- -0 12 34 5- -- -Sample Output 1:
YES 8Sample Input 2:
8- -4 50 6- -2 3- 7- -- -Sample Output 2:
NO 1
问一棵树是不是完全二叉树,直接给根节点赋值1,然后左边是2,右边是3,这样判断最大的值是不是大于n即可
#include<cstdio>#include<vector>#include<queue>#include<string>#include<map>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int INF = 0x7FFFFFFF;const int maxn = 1e5 + 10;int n, ch[maxn][2], flag[maxn], ans;char s[maxn];int get(){scanf("%s", s);return s[0] == '-' ? -1 : s[1] ? s[1] - '0' + 10 * (s[0] - '0') : s[0] - '0';}bool dfs(int x, int y){if (x == -1) return true;if (y > n) return false;if (y == n) ans = x;return dfs(ch[x][0], y << 1) && dfs(ch[x][1], y << 1 | 1);}int main(){scanf("%d", &n);for (int i = 0; i < n; i++){ch[i][0] = get();if (ch[i][0] != -1) flag[ch[i][0]] = 1;ch[i][1] = get();if (ch[i][1] != -1) flag[ch[i][1]] = 1;}for (int i = 0; i < n; i++){if (!flag[i]){if (dfs(i, 1)) printf("YES %d\n", ans);else printf("NO %d\n", i);}}return 0;}
- PAT (Advanced Level) Practise 1110 Complete Binary Tree (25)
- PAT (Advanced Level) Practise 1110 Complete Binary Tree (25)
- PAT (Advanced Level) Practise 1064 Complete Binary Search Tree (30)
- PAT (Advanced Level) Practise 1064 Complete Binary Search Tree (30)
- PAT (Advanced Level) Practise 1102 Invert a Binary Tree (25)
- PAT (Advanced Level) Practise 1102 Invert a Binary Tree (25)
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
- 【PAT】【Advanced Level】1110. Complete Binary Tree (25)
- PAT-PAT (Advanced Level) Practise 1102 Invert a Binary Tree (25)【三星级】
- Pat(Advanced Level)Practice--1064(Complete Binary Search Tree)
- 【PAT】【Advanced Level】1064. Complete Binary Search Tree (30)
- PAT-Advanced 1110. Complete Binary Tree (25)
- 浙大PAT (Advanced Level) Practise 1043Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25)【二叉树】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1043 Is It a Binary Search Tree (25)
- 1102. Invert a Binary Tree (25)【二叉树】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1043 Is It a Binary Search Tree (25)
- PAT (Advanced Level) Practise 1123Is It a Complete AVL Tree (30)
- 时钟
- 冒泡排序
- java.lang.NoSuchMethodException 的解决方法们(转)
- 使用javah快速生成本地方法及生成so动态库文件
- 我对MVP的简单理解
- PAT (Advanced Level) Practise 1110 Complete Binary Tree (25)
- 时间日期
- 加载配置文件
- C#序列化,反序列化(Serialize,Deserialize)
- Java对象初始化
- nyoj364(贪心算法~好题)
- 使用NDK时的一些小知识点
- bootstrap使用心得总结
- iOS View 指定圆角位置