03-树2 List Leaves(25 point(s)) 【Tree】
03-树2 List Leaves(25 point(s))
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
思路
广度优先搜索 往下搜 遇到 没有儿子的 压入 一个 vector 就可以
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
struct Node
{
int l, r;
}q[10];
queue <int> Q;
vector <int> ans;
void bfs()
{
int len = Q.size();
for (int i = 0; i < len; i++)
{
int num = Q.front();
Q.pop();
if (q[num].l != -1 && q[num].r != -1)
{
Q.push(q[num].l);
Q.push(q[num].r);
}
else if (q[num].l != -1)
Q.push(q[num].l);
else if (q[num].r != -1)
Q.push(q[num].r);
else
ans.pb(num);
}
if (Q.size())
bfs();
}
int main()
{
int n;
scanf("%d", &n);
char a, b;
map <int, int> m;
for (int i = 0; i < n; i++)
{
scanf(" %c %c", &a, &b);
if (a != '-')
{
q[i].l = a - '0';
m[a - '0'] = 1;
}
else
q[i].l = -1;
if (b != '-')
{
q[i].r = b - '0';
m[b - '0'] = 1;
}
else
q[i].r = -1;
}
int root;
for (int i = 0; i < n; i++)
{
if (m[i] == 0)
{
root = i;
break;
}
}
Q.push(root);
bfs();
vector <int>::iterator it;
for (it = ans.begin(); it != ans.end(); it++)
{
if (it != ans.begin())
printf(" ");
printf("%d", (*it));
}
printf("\n");
}
03-树2 List Leaves(25 point(s)) 【Tree】的更多相关文章
- 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 ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- pat03-树2. List Leaves (25)
03-树2. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a t ...
- 03-树1 树的同构(25 point(s)) 【Tree】
03-树1 树的同构(25 point(s)) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Django REST framework 的TokenAuth认证及外键Serializer基本实现
一,Models.py中,ForeignKey记得要有related_name属性,已实现关联对象反向引用. app_name = models.ForeignKey("cmdb.App&q ...
- facebook面试题【转】
1. 给两个类A和Bclass A {public void foo (A a) { ...}}class B extends A {public void foo (B b) { ...}}问这么写 ...
- javascript 对象初探 (六)--- call()和apply()初探
在javascript中,每个函数都具有call()和apply()两个方法,您可以用她们来触发函数,并指定相关的调用参数. 此外,这两个方法还有另一个功能,就是她可以让一个对象去‘借用‘另一个对象的 ...
- JS中原型链中的prototype与_proto_的个人理解与详细总结(**************************************************************)
一直认为原型链太过复杂,尤其看过某图后被绕晕了一整子,今天清理硬盘空间(渣电脑),偶然又看到这图,勾起了点回忆,于是索性复习一下原型链相关的内容,表达能力欠缺逻辑混乱别见怪(为了防止新人__(此处指我 ...
- session再次理解
1.session介绍: session主要用来存储用户的会话所需的信息(用户行为信息),当用户在同一个服务器上实现不同的操作时,session信息会以变量的形式存储在服务器的内存中,保存用户的状态信 ...
- Git学习0基础篇(下)
server上的 Git - 协议 Git能够使用四种基本的协议传输资料:本地协议(Local).HTTP 协议.SSH(Secure Shell) 协议以及 Git 协议.眼下使用最普及的是 SSH ...
- jquery 的父子节点
1.一级父节点 parent() n级父节点 parents(???). 2.一级子节点 children() n级子节点 用find(???)
- HRBUST2030(dfs)
成语接龙 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class nam ...
- mysql手动停止无响应查询方法
http://www.chenweionline.cn/archives/61.htm
- 数据挖掘 与 Web开发何去何从
(0)引子 以下以现实生活中的一个实例引出本博客的探究点.或许类似的情况正发生在你的身边. 小弟工作5年了,近期有点迷茫. 上一份工作在一家比較大的门户站点做web开发和移动互联网数据挖掘(人手比較紧 ...