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.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤) 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<queue>
using namespace std;
const int maxn = ;
struct Node{
int lchild,rchild;
}node[maxn];
int h[maxn],leaf[maxn],num = ;
bool isRoot[maxn]; int charToint(char c){
if(c == '-') return -;
else{
isRoot[c-''] = false;
//printf("isRoot[%d] == %d\n",c-'0',isRoot[c-'0']);
return c-'';
}
} void print(int i){
if(num == ){
printf("%d",i);
num++;
}
else printf(" %d",i);
} void BFS(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
//printf("1\n");
int now = q.front();
q.pop();
if(node[now].lchild == - && node[now].rchild == -) print(now);
if(node[now].lchild != -) q.push(node[now].lchild);
if(node[now].rchild != -) q.push(node[now].rchild);
}
} int main(){
int n;
scanf("%d\n",&n);
for(int i = ; i < n; i++){
isRoot[i] = true;
}
char a,b;
for(int i = ; i < n; i++){
scanf("%c %c",&a,&b);
getchar();
int u = charToint(a);
int v = charToint(b);
node[i].lchild = u;
node[i].rchild = v;
}
int root;
for(int i = ; i < n; i++){
if(isRoot[i] == true) root = i;
}
//printf("root == %d\n",root); BFS(root);
return ;
}
03-树2 List Leaves (25 分)的更多相关文章
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- 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 ...
- 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 ...
- L2-006 树的遍历 (25 分)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...
- 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 ...
- 浙大数据结构课后习题 练习三 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 ...
- 7-3 树的同构(25 分) JAVA
给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...
- PTA 03-树1 树的同构 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构 (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...
- PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The heig ...
- PTA 树的同构 (25分)
PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...
随机推荐
- vmware workstation + kvm 部署
1.物理机BIOS设置里开启虚拟化功能 2.vm里面开启一下两个功能 3.找到存放该虚拟机的dir,找到以.vmx结尾的文件,增加一行 apic.xapic.enabled = "FALSE ...
- Log4J 配置 详解
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...
- MySQL性能调优与架构设计——第3章 MySQL存储引擎简介
第3章 MySQL存储引擎简介 3.1 MySQL 存储引擎概述 MyISAM存储引擎是MySQL默认的存储引擎,也是目前MySQL使用最为广泛的存储引擎之一.他的前身就是我们在MySQL发展历程中所 ...
- sql 与 oracle 几个简单语法差别
sql 与 oracle 之间的 语法差别. 简单的几个函数转换 sql-> Up_Time=getdate(), isnull(), substring(), charindex(), ...
- MVC 基本概念
1. M(Model-模型): 数据模型. 负责定义信息格式与信息反问的界面,包括商业逻辑,数据反问.(可以理解成是三层模式中的 BLL+DAL ) 2. V(View-视图): 负责用户界面 UI ...
- PostBack
PostBack 字面意义 Post提交 Back回来. 提交回来. 1. AutoPostBack 服务器控件需要设置 AutoPostBack="true" 后才会提交服务器. ...
- 以太坊系列之二: 单调时间monotime-以太坊源码学习
在程序中需要测量时间时最好使用monotime.Now()而不是time.Now(),相比之下前者更准确. 来个示例: func main() { var start, elapsed time.Du ...
- PHP7 - MongoDB Driver 使用心得
php7 只能使用Mongodb driver来驱动mongodb. 使用Mongodb Driver连接数据库 刚开始使用Mongodb Driver的时候我是拒绝的.查看官方文档只看到一排的类和不 ...
- scrapy 调试功能
在使用 scrapy 来爬取网页的时候,我们难免会使用到调试功能,下面介绍两种调试方法: 1.终端使用 scrapy shell exampleurl exampleurl 为你要爬取网站的 url ...
- JavaWeb应用中初始化Log4j的两种方式
本文主要介绍了普通JavaWeb应用(基于Tomcat)中初始化Log4j的两种方式: 1.通过增加 InitServlet ,设置令其自启动来初始化 Log4j . 2.通过监听器 ServletC ...