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 ...
随机推荐
- JsLint 的安装和使用
JSLint 是一款Javascript验证工具,在定位错误并确保基本指南得以遵循时,非常有用.如果你正在编写专业级的javascript,应该使用 JSLint 或者类似的验证工具(JSHint). ...
- Chromimu与JS交互的测试
CHROMIMU与JS交互的测试 好东西 谷歌浏览器 学习 研究 http://blog.csdn.net/grassdragon/article/details/51659798 Chromimu ...
- Guava cache 示例
pom.xml <!-- guava --> <dependency> <groupId>com.google.guava</groupId> < ...
- fork多线程进程时的坑(转)
add : 在fork多线程的进程时,创建的子进程只包含一个线程,该线程是调用fork函数的那个线程的副本.在man fork中,有The child process is created with ...
- C++取出string的一部分以及int型转成string类型
在实际应用中,对文件的操作是十分频繁的,我们需要对文件进行拷贝,重命名等操作,这是就需要获取文件的绝对路径,一般情况下,该路径是以字符串的形式存储的,如果我们需要对文件进行重命名等,就需要对绝对路径这 ...
- 第一章:深入.NET框架
1..net框架结构 主要包含公共语言运行时(CLR)和框架类库(.NET Framework 类库 ,FCL) 2.CLR 1.对于一个将要面向.NET平台进行开发的人来说,了解一下.NET平台的 ...
- es修改指定的field(partial update)
PUT /index/type/id 创建文档&替换文档,就是一样的语法一般对应到应用程序中,每次的执行流程基本是这样的:1.应用程序发起一个get请求,获取到document,展示到前台界面 ...
- 运行Maven工程中修改tomcat端口
Maven 运行:clean tomcat7:run 若需要修改端口,则用clean -Dmaven.tomcat.port=8082 tomcat7:run
- office excel中怎么添加批注及修改批注用户名
office excel中怎么添加批注及修改批注用户名 参考:https://jingyan.baidu.com/article/c33e3f48a52853ea15cbb5db.html 1. of ...
- SUDO安全委派和安全模块
sudo更换身份 su 切换身份 使用su 切换身份必须首先直到被切换成用户的密码 如: su root 就必须要知道root的密码 这种机制安全性不高,容易泄露管理员密码 1. sudo ...