【PAT】【Advanced Level】1110. Complete Binary Tree (25)
来源:互联网 发布:氰化钠 淘宝 编辑:程序博客网 时间:2024/05/21 19:30
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
原题链接:
https://www.patest.cn/contests/pat-a-practise/1110
思路:
树的遍历
满二叉树的特征
CODE:
#include<iostream>#include<cstdlib>#include<cstring>#include<string>#include<vector>#include<algorithm>#define N 22using namespace std;typedef struct S{int ind;int ls;int rs;int s;int fa;int tra;int fl;};S t[N];int tr;vector<int> ro;vector<int> le;vector<S> trav;void dfs1(int n,int flo){t[n].fl=flo;if (t[n].ls!=-1){dfs1(t[n].ls,flo+1);}t[n].tra=tr;tr++;trav.push_back(t[n]);if (t[n].rs!=-1){dfs1(t[n].rs,flo+1);}return ;}bool cmp(S a,S b){if(a.fl==b.fl){return a.tra<b.tra;}else{return a.fl<b.fl;}}int main(){int n;cin>>n;for (int i=0;i<n;i++) t[i].fa=-1;for (int i=0;i<n;i++){t[i].ind=i;t[i].s=0;string a,b;cin>>a>>b;if (a!="-"){t[i].ls=atoi(a.c_str());t[t[i].ls].fa=i;t[i].s++;}else{t[i].ls=-1;}if (b!="-"){t[i].rs=atoi(b.c_str());t[t[i].rs].fa=i;t[i].s++;}else{t[i].rs=-1;}//cout<<t[i].s<<endl;if (t[i].s!=0){ro.push_back(i);}else{le.push_back(i);}}int root;for (int i=0;i<n;i++){if (t[i].fa==-1){root=i;break;}}tr=0;dfs1(root,0);sort(trav.begin(),trav.end(),cmp);//cout<<trav[n-1].ind<<endl;int flag=0;for (int i=0;i<ro.size();i++){if (t[ro[i]].s!=2){if (!(t[ro[i]].ls==trav[n-1].ind)){//cout<<ro[i]<<" "<<t[ro[i]].ls<<" "<<t[ro[i]].rs<<endl;flag=1;break;}}}if (flag==0){for (int i=0;i<le.size();i++){if (t[le[i]].fl<(trav[n-1].fl-1)){flag=1;break;}else if (t[le[i]].fl==(trav[n-1].fl-1)){if (t[le[i]].tra<trav[n-1].tra){flag=1;break;}}}}if (flag==0){cout<<"YES "<<trav[n-1].ind<<endl;}else{cout<<"NO "<<root<<endl;}return 0;}
- 【PAT】【Advanced Level】1110. Complete Binary Tree (25)
- PAT-Advanced 1110. Complete Binary Tree (25)
- PAT (Advanced Level) Practise 1110 Complete Binary Tree (25)
- PAT (Advanced Level) Practise 1110 Complete Binary Tree (25)
- Pat(Advanced Level)Practice--1064(Complete Binary Search Tree)
- PAT (Advanced Level) Practise 1064 Complete Binary Search Tree (30)
- PAT (Advanced Level) Practise 1064 Complete Binary Search Tree (30)
- 【PAT】【Advanced Level】1064. Complete Binary Search Tree (30)
- PAT (Advanced Level) 1064. Complete Binary Search Tree (30) 递归建立CBST
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1102 Invert a Binary Tree (25)
- PAT (Advanced Level) Practise 1102 Invert a Binary Tree (25)
- 【PAT】【Advanced Level】1102. Invert a Binary Tree (25)
- [pat]1110. Complete Binary Tree (25)
- pat 1110. Complete Binary Tree (25)
- PAT(A) - 1110. Complete Binary Tree (25)
- PAT 1110. Complete Binary Tree (25)
- pat 1110. Complete Binary Tree (25)
- kafka分布式消息队列使用(springboot和springmvc)
- vagrant下配置centos7+mariadb数据库并支持外部访问
- class类的静态和动态加载
- LeetCode 617. Merge Two Binary Trees (Easy)
- js分几部分
- 【PAT】【Advanced Level】1110. Complete Binary Tree (25)
- 1005 number number number
- 重构二叉树
- jquery+bootstrap 实现简单备忘录(一)
- (noip 2013 华容道)<搜索>
- map大梗概
- Redis 和 Memcached 的区别
- linux 2.6升级Python2.7 ./configure 报错问题
- Spark算子讲解(一)