题目

分析

输入先给出结点的数量,把结点从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)的更多相关文章

  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. pat03-树2. List Leaves (25)

    03-树2. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a t ...

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

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

  5. 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 ...

  6. 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. 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 ...

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

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

  9. 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 ...

  10. 浙大数据结构课后习题 练习三 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. RMAN.DBMS_RCVCAT 版本错误处理

    登录新装的catalog库准备注册数据库时报版本问题 rman target / catalog rman11g/rman2010#@rman Recovery Manager:Release 11. ...

  2. 可变数组(PLSQL)

    可变数组 可变数组与嵌套表相似,也是一种集合.一个可变数组是对象的一个集合,其中每个对象都具有相同的数据类型.可变数组的大小由创建时决定.在表中建立可变数组后,可变数组在主表中作为一个列对待.从概念上 ...

  3. Mybatis批量insert 返回主键值和foreach标签详解

    Mybatis批量insert 返回主键 Mybatis从3.3.1版本开始,支持批量插入后返回主键ID.首先对于支持自增主键的数据库使用useGenerateKeys和keyProperty,对于不 ...

  4. SSH的三种端口转发

    1.本地转发 本地转发,顾名思义就是把本地主机端口转发到远程主机端口. ssh -L 本地主机端口:远程主机:远程主机端口 举例:ssh -L 50000:www.google.com:80 user ...

  5. 4.0-uC/OS-III目录结构

    本文章都是基于学习野火STMF4系列的开发板的学习做的,大部分都是开发手册的内容,做笔记用,具体请参考野火官方的开发手册. 1. uC/OS-III 文件结构 ①配置文件,通过定义这些文件里宏的值可以 ...

  6. Linux ethtool 命令

    ethtool 是用于查询及设置网卡参数的命令,常见用法如下: 注意:该命令只是临时设置,如果网卡重启就失效了,如果想要永久保存应该配置 /etc/sysconfig/network-scripts/ ...

  7. mysql 5.7配置项最详细的解释

    配置样例 首先提供一个我使用的配置样例 [client] #password=88888888 socket=/data/var/mysql/mysql.sock [mysqld_safe] pid- ...

  8. cxLookupComboBox使用方法

    示例 //选择修改时执行procedure TForm1.cxLookupComboBox1PropertiesChange(Sender: TObject); begin edit1.Text:=V ...

  9. 永久有效的 webstorm license server 20180808

    下载地址  https://download.jetbrains.com/webstorm/WebStorm-2018.3.2.exe 2018年10月26日,最近老是过期,搞了一个1年有效的代码,是 ...

  10. ie6-ie8支持CSS3选择器的解决办法

    引入nwmatcher.js和selectivizr.js <!--[if lt IE 10]> <script src="html5shiv.js">&l ...