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 ...
随机推荐
- Page14:状态观测器[Linear System Theory]
内容包含状态观测器设计分类及其特点 状态观测器设计与状态反馈之间的关系问题 带补偿器动态输出反馈与带状态观测器的状态反馈等价原理 由于第二部分频域部分较为简单,因此仅整理时间域部分
- [skill][vim] 常用技巧与配置
一: 光标行列高亮 可以使用 :help highlight 查看相信帮助可颜色配置. set cursorline set cursorcolumn highlight Cursorline ct ...
- [administrative][qemu][kvm] qemu使用--bridge-helper
公司服务器,源码安装的 qemu-2.9.0. 不是yum装的. 问题1:非超级用户的kvm权限问题: 略,直接超级用户使用. 也许有用? https://access.redhat.com/docu ...
- Copycat - StateMachine
看下用户注册StateMachine的过程, CopycatServer.Builder builder = CopycatServer.builder(address); builder.withS ...
- 树和二叉树->基础知识
树的表示方法 1 一般表示法 2 广义表表示法 3 凹入表示法 树的基本术语: 树:n(n>=0)个结点的有限集 结点:包含一个数据元素及若干指向其子树的分支 结点的度:结点拥有的子树数成为结点 ...
- Linux and Oracle常用目录详解
目录详解 目录 内容 / 根目录,一切从这里开始 /bin 包含系统启动和运行所必需的二进制文件(程序) /boot 包含Linux内核.最初的RAM磁盘映像(系统启动时,驱动程序会用到),以及启动加 ...
- falsk 与 django 过滤器的使用与区别
1,flask中内置的过滤器模板中常用方法: {#过滤器调用方式{{变量|过滤器名称}} #} <!-- safe过滤器,可以禁用转义 --> {{'<strong>hello ...
- Android activity 周期图和fragment周期图
与Activity生命周期的对比 Fragment的生命周 onCreateView():每次创建.绘制该Fragment的View组件时回调该方法,Fragment将会显示该方法返回的View组件. ...
- rpm 安装软件包
RPM 是RedHat Package Manager (RedHat软件包管理工具)类似windows里面的“添加/删除程序”,rpm执行安装包 RPM执行安装.删除.更新 常用命令组合 -ivh: ...
- dedecms自定义表单提交成功后提示信息修改和跳转链接修改
我们在用dedecms自定义表单提交成功后提示信息一般是"Dedecms 提示信息",这个要怎么改成自己想要的文字呢?还有就是提示页停留时间,目前估计就2秒,太快了,要如何设置长点 ...