03-树2 List Leaves(25)
题目

分析
输入先给出结点的数量,把结点从0开始标号,每一行给出结点的左右两个子节点,-表示子节点不存在。
很容易分析出在子节点中没有出现的就是根节点,两个子节点都为空的是叶子节点
先建树,然后从root结点广度优先搜索,搜索到叶子节点就搜索,需要注意的是因为要求输出的顺序是从上到下、从左到右,因此如果左右子节点都不为空则应先push左子节点。

AC代码
#include "bits/stdc++.h"
using namespace std;
struct TreeNode
{
int left, right;
}tree[14];
int main() {
int n, i;
cin >> n;
string l, r;
bool isRoot[14];
memset(isRoot, 1, sizeof(isRoot));
for (i = 0; i < n; i++) {
cin >> l >> r;
if (l[0] != '-') {
tree[i].left = stoi(l);
isRoot[tree[i].left] = 0;
}
else
tree[i].left = -1;
if (r[0] != '-') {
tree[i].right = stoi(r);
isRoot[tree[i].right] = 0;
}
else
tree[i].right = -1;
}
//找到根结点
int root;
for (i = 0; i < n; i++) {
if (isRoot[i]) root = i;
}
//cout << "根节点: " << root << endl;
queue<int> q;
q.push(root);
vector<int> v;
while (!q.empty()) {
int t = q.front();
//cout << t << ' ';
q.pop();
if (tree[t].left == -1 && tree[t].right == -1)
v.push_back(t);
if (tree[t].left != -1)
q.push(tree[t].left);
if (tree[t].right != -1)
q.push(tree[t].right);
}
//cout << endl;
for (i = 0; i < v.size(); i++) {
cout << v[i];
if (i != v.size() - 1)
cout << ' ';
}
return 0;
}
03-树2 List Leaves(25)的更多相关文章
- 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 ...
随机推荐
- [administrative][CentOS][NetworkManager] 万恶的NetworkManager到底怎么用
这好像是第三次不得不去学会NetworkManager的用法,可是它真的很难用.社区里无人不吐槽. 然而,还是要用! 这次从redhat的文档入手,也许可以成功 --! https://access. ...
- 流计算技术实战 - CEP
CEP,Complex event processing Wiki定义 "Complex event processing, or CEP, is event processing that ...
- Blender 使用
教程: 1.https://www.youtube.com/watch?v=N8-mE-165b8&index=7&list=PLE885296A496C3D38 快捷键: http: ...
- cocoapod引入FLEX,debug模式正常,Release报错library not found for -lXXX
cocoapod引入FLEX,debug模式正常,Release报错library not found for -lXXX, 因为podfile是这么写的: pod 'FLEX', '~> 2. ...
- 转:Web项目的WEB-INF目录使用说明以及重定向与转发
原文地址:https://www.cnblogs.com/shenxiaoquan/p/5819359.html 原文内容: 总结一下这篇文章的内容: WEB-INF下面的内容都是只能由服务器级别才能 ...
- Numpy 机器学习三剑客之Numpy
NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.Numpy内部解除了Python的PIL(全局解释器锁),运算效率极好,是大量机 ...
- vue中封装一个全局的弹窗js
/** * Created by yx on 2017/12/21. */ export default { /** * 带按钮的弹框 * <!--自定义提示标题,内容,单个按钮事件--> ...
- 洛谷P4495 奇怪的背包 [HAOI2018] 数论
正解:数论+dp 解题报告: 传送门! 首先看到这题,跳无数次,自然而然可以想到之前考过好几次了的一个结论——如果只考虑无限放置i,它可以且仅可以跳到gcd(p,v[i]) 举一反三一下,如果有多个i ...
- CentOS 7 下安装jdk1.8(转)
原文:https://blog.argcv.com/articles/3155.c CentOS 7下目前默认是jdk1.6和1.7.若需要更高版本的1.8,我们就需要一点额外的手段了. 首先,我们需 ...
- opencv手工编译
opencv手工编译方法1.下载cmake gui2.在where is the source code路径下配置opencv根目录,在where to build the binaries路径下配置 ...