03-树2. List Leaves (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) 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 test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5

提交代码

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
struct node{
int l,r,f;
node(){
l=r=f=-;
}
};
node tree[];
/*void prefind(int h){//检验程序
cout<<h<<endl;
if(tree[h].l>=0)
prefind(tree[h].l);
if(tree[h].r>=0)
prefind(tree[h].r);
}*/
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
//freopen("D:\\OUTPUT.txt","w",stdout);
int n;
string s;
scanf("%d",&n); //cout<<n<<endl; int i,num,j; /*for(i=0;i<n;i++){
cout<<"f: "<<tree[i].f<<endl;
cout<<"l: "<<tree[i].l<<endl;
cout<<"r: "<<tree[i].r<<endl;
}*/ for(i=;i<n;i++){
cin>>s;
if(s!="-"){
num=;
j=;
while(j<s.length()){
num*=;
num+=s[j++]-'';
}
tree[i].l=num;
tree[num].f=i;
}
cin>>s;
if(s!="-"){
num=;
j=;
while(j<s.length()){
num*=;
num+=s[j++]-'';
}
tree[i].r=num;
tree[num].f=i;
}
}
int h;
for(i=;i<n;i++){
if(tree[i].f<){
h=i;
break;
}
} //cout<<h<<endl;
//prefind(h); int p;
queue<int> q,qq;
q.push(h);
while(!q.empty()){
p=q.front();
q.pop(); //cout<<p<<endl; if(tree[p].l>=||tree[p].r>=){
if(tree[p].l>=)
q.push(tree[p].l);
if(tree[p].r>=)
q.push(tree[p].r);
}
else{
qq.push(p);
}
} //cout<<h<<endl; if(!qq.empty()){
printf("%d",qq.front());
qq.pop();
}
while(!qq.empty()){
printf(" %d",qq.front());
qq.pop();
}
printf("\n");
return ;
}

pat03-树2. List Leaves (25)的更多相关文章

  1. 03-树2. List Leaves (25) 二叉树的层序遍历

    03-树2. List Leaves (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912 Given a tree, you a ...

  2. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  3. 03-树1 树的同构(25 point(s)) 【Tree】

    03-树1 树的同构(25 point(s)) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为 ...

  4. PTA 03-树2 List Leaves (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves   (25分) Given a tree, you ...

  5. 03-树1. List Leaves (25)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  6. 7-4 List Leaves (25分) JAVA

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  7. L2-006 树的遍历 (25 分)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...

  8. 03-树2 List Leaves (25 分)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  9. 浙大数据结构课后习题 练习三 7-4 List Leaves (25 分)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

随机推荐

  1. JS判断键盘是否按的回车键并触发指定按钮点击操作

    document.onkeydown = function (e) { if (!e) e = window.event; if ((e.keyCode || e.which) == 13) { va ...

  2. WinForm中ListView的使用

    每一行是一个ListViewItem对象,每一项是一个ListViewSubItem对象 样式 整行选择:this.lvDataSourceSearchHistory.FullRowSelect = ...

  3. Window 7 Professional 多语言设置

    1. 正常情况下,WINDOW系统只提供企业和旗舰版的语言切换的界面设置,其他版本没有. 2. 首先下载语言包,然后解压待用. 3. 以管理员身份运行命令窗口,如下输入: 4. 上面完成后,下载 ht ...

  4. java集合类学习笔记之LinkList

    1.简述 LinkList的底层其实就是一个双向链表,所谓的链表就是一个LinkList内部静态静态类(Node),对LinkList的所有操作本质上就是通过对LinkList中新建的Node对象 进 ...

  5. 八大排序算法的python实现(三)冒泡排序

    代码: #coding:utf-8 #author:徐卜灵 #交换排序.冒泡排序 L = [1, 3, 2, 32, 5, 4] def Bubble_sort(L): for i in range( ...

  6. gym 102082B dp

    和51nod1055 一样: #include<iostream> #include<cstdio> #include<algorithm> #include< ...

  7. JDK 5 ~ 10 新特性倾情整理!

    JDK 5 ~ 10 新特性倾情整理! 最近连 JDK11都在准备发布的路上了,大家都整明白了吗?也许现在大部分人还在用6-8,8的新特性都没用熟,9刚出不久,10-11就不用说了. 为了大家对JDK ...

  8. linux下的小命令

    (1) 查看服务器的IP信息 ip add show ifconfig (2) 操作网卡命令(重启网络和启用网卡) cleasystemctl restart network systemctl st ...

  9. 004 Android XML文件常用五大页面布局方式

    1.线性布局(LinearLayout)最常用 <1>使用线性布局,首先在xml文件中修改布局为LinearLayout 修改完成后,可在Component Tree中看见如下内容: &l ...

  10. css3 超出文本...显示

    width: 250px 或者 100%; max-width: 285px; overflow: hidden; display: inline-block; white-space: nowrap ...