题目

分析

输入先给出结点的数量,把结点从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. shell 文件描述符

    /tmp/test.sh > /tmp/test.log 2>&1 这个命令的意思是 前半部分是将shell的输出重定向到/tmp/test/log.默认是标准输出(stdout文 ...

  2. ida pro65

    https://elinux.org/CI20_Dev_Zone#Making_a_bootable_SD_card_from_sources IDAPro65.exe: 下载地址:http://pa ...

  3. [daily] 内存越界的分析与定位

    valgrind 自不必说 1.  Address Sanitize 很好有,只需要在gcc编译的时候,加上选项 -fsanitize=address 它的工程:https://github.com/ ...

  4. Maven项目Update Project后JRE System Library自动变回1.5解决办法

    最近在搭建Spring Boot项目<一步步搭建 Spring Boot maven 框架的工程>的时候,虽然设置JRE System Library为1.8,但是,当我 用 Maven ...

  5. There are 0 datanode(s) running and no node(s) are excluded in this operation.

    向hadoop导入文件,报错 .... There are 0 datanode(s) running and no node(s) are excluded in this operation. . ...

  6. Scala笔记

    定义包 package com.runoob { class HelloWorld } 引用包 import java.awt.Color // 引入Color import java.awt._ / ...

  7. 使用poi写excel文件

  8. 第一章:HTML5的基础

    HTML5基础 1.DoctYpe声明 <!DCTYPE html>必须放在第一行. <title> <title> 百度</title> <me ...

  9. 如何修改帝国cms文章点击量默认值和成倍增加

    我们在帝国cms发布完文章,在不点击的情况下,“点击量”默认显示为0,请问有什么方法,修改关注默认值吗?这个可以在增加信息时,“特殊属性”标签里修改点击量,如下图 有朋友问有没其他好的方法快速增加.成 ...

  10. Mac提醒事项如何设置为24小时制