当前位置:网站首页>PTA 7-3 lists leaf nodes
PTA 7-3 lists leaf nodes
2022-07-27 00:09:00 【Muxi Krystal】
https://pintia.cn/problem-sets/1259041418971803648/problems/1259046259496415233
( Title link above )
Answer key :
1, Store with structure array , Be careful char Convert to int.
2, utilize vis The array records the child nodes that have appeared , Then what does not appear is the root node .
3, Finally, sequence traversal ( Wide search ) Output leaf node .
AC Code :
#include <iostream>
#include <queue>
using namespace std;
int leave[15],vis[30];
struct treenode{
int num;
int left,right;
}node[15];
int bfs(int rt){
queue<treenode>q;
q.push(node[rt]);
int k=0;
while(!q.empty()){
treenode root=q.front();
q.pop();
int l,r;
l=root.left; r=root.right;
if(l==-1&&r==-1){
leave[k++]=root.num;
}
if(l!=-1) q.push(node[l]);
if(r!=-1) q.push(node[r]);
}
return k;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
char l,r;
cin>>l>>r;
node[i].num=i;
if(l=='-') node[i].left=-1;
else{
node[i].left=l-'0';
vis[l-'0']=1;
}
if(r=='-') node[i].right=-1;
else{
node[i].right=r-'0';
vis[r-'0']=1;
}
}
int rt;
for(int i=0;i<n;i++){
if(!vis[i]){
rt=i;break;
}
}
int k;
k=bfs(rt);
for(int i=0;i<k;i++){
if(i==0) cout<<leave[i];
else cout<<" "<<leave[i];
}
cout<<endl;
return 0;
}
边栏推荐
- Dajiang Zhitu and CC have produced multiple copies of data. How to combine them into one and load them in the new earth map
- In depth interpretation of the investment logic of the consortium's participation in the privatization of Twitter
- np. transpose & np.expand_ dims
- Dynamic SQL
- Familiarize you with the "phone book" of cloud network: DNS
- 【C语言】数组
- 11_ Weather case - monitoring properties
- Part II - C language improvement_ 10. Function pointer and callback function
- Upload files to the server
- Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching
猜你喜欢

Chapter 7 course summary

Part II - C language improvement_ 13. Recursive function

04 traditional synchronized lock

关于可穿戴式NFT你应该知道的一切!

Practice of data storage scheme in distributed system

08 design of intelligent agricultural environmental monitoring system based on ZigBee
![[C language] array](/img/b7/fe090984af689e45cf3492ff8d4c61.png)
[C language] array

Azure Synapse Analytics 性能优化指南(3)——使用具体化视图优化性能(下)

力扣141题:环形链表

Tencent cloud lightweight application server purchase method steps!
随机推荐
Upload files to OSS file server
Chapter 2 develop user traffic interceptors
买不到的数目
百度网址收录
动态sql
Dajiang Zhitu and CC have produced multiple copies of data. How to combine them into one and load them in the new earth map
Complete backpack and 01 Backpack
What is Tencent cloud lightweight application server? What are the differences between CVM and ECS?
Skiasharp's WPF self drawn bouncing ball (case version)
Modulo (remainder) operation in the range of real numbers: how to find the remainder of negative numbers
文件上传到OSS文件服务器
[literature reading] hat: hardware aware transformers for efficient natural language processing
Method of setting QQ to blank ID
Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching
【面试:并发篇26:多线程:两阶段终止模式】volatile版本
Question 152: product maximum subarray
Pytorch learning record (II): tensor
Tensorflow2.0 deep learning simple tutorial of running code
Bid farewell to wide tables and achieve a new generation of Bi with DQL
Section 6: introduction to cmake grammar