PTA 03-树2 List Leaves(25 分)
来源:互联网 发布:阿国网络随笔诈骗 编辑:程序博客网 时间:2024/04/30 00:32
先是找到根节点在哪,然后层序遍历;
#include<iostream>#define MaxTree 10#define Tree char #include<vector>#include<queue>using namespace std;struct TreeNode{int index;Tree Left;Tree Right;int leftnumber;int rightnumber;}T1[MaxTree];queue<TreeNode>q;int main(){int n;cin>>n;int check[10]; for(int i=0;i<n;i++) check[i]=0;for(int i=0;i<n;i++){T1[i].index=i; cin>>T1[i].Left>>T1[i].Right;if(T1[i].Left!='-'){T1[i].leftnumber=T1[i].Left-'0';check[T1[i].leftnumber]=1; }else T1[i].leftnumber=-1; if(T1[i].Right!='-'){T1[i].rightnumber=T1[i].Right-'0';check[T1[i].rightnumber]=1;}else T1[i].rightnumber=-1; }int Root;for(int i=0;i<n;i++)//找根结点 if(!check[i]) { Root=i; break; }//cout<<Root<<endl; q.push(T1[Root]); int count = 0; //cout<<"hello world"<<endl; while(!q.empty())//层序遍历 { TreeNode heel =q.front(); q.pop(); if(heel.leftnumber!=-1) { q.push(T1[heel.leftnumber]); }if(heel.rightnumber!=-1){q.push(T1[heel.rightnumber]);}if(heel.leftnumber==-1&&heel.rightnumber==-1){if(count == 0){cout<<heel.index;count=1;}else cout<<" "<<heel.index;}}return 0;}
阅读全文
0 0
- PTA 03-树2 List Leaves(25 分)
- [pta]03-树2 List Leaves (25分)
- PTA 03-树2 List Leaves
- PTA--03-树2 List Leaves
- PTA 03-树2 List Leaves
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- HttpURLConnection---网络请求数据
- 内存管理,malloc,calloc,realloc的使用
- Shiro入门学习一
- mac安装ipython
- 生产者消费者问题
- PTA 03-树2 List Leaves(25 分)
- c代码 位操作 强化练习
- 设计模式
- POJ 3468
- Docker下centos7镜像安装中文支持
- Linux mysql5.7.20 二进制文件安装 添加服务为自启动
- 微信小程序地图map的markers属性polyline属性controls属性全解析教程
- python 重写 json类
- 大家好 第一次写博客 css的基础分享给大家