问题

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

解答

#include <iostream>
#include <queue>
using namespace std;template <typename T>
class Node
{
public:T data;int left;int right;
};template <typename T>
class Tree
{
public:unsigned N;Node<T> *tree;int root;Tree(int n) : N(n){if (n == 0){N = 0;tree = nullptr;root = -1;return;}root = 0;tree = new Node<T>[N];char l, r;for (int i = 0; i < n; ++i){cin >> l >> r;tree[i].data = i;tree[i].left = l == '-' ? -1 : l - '0';tree[i].right = r == '-' ? -1 : r - '0';if (tree[i].left != -1)root ^= tree[i].left;if (tree[i].right != -1)root ^= tree[i].right;}for (int i = 0; i < n; ++i)root ^= i;}void find_leaves(){queue<int> result;if (N == 0)return;queue<Node<unsigned>> Q;Q.push(tree[root]);Node<unsigned> p;while (!Q.empty()){p = Q.front();if (p.left + p.right == -2)result.push(p.data);Q.pop();if (p.left != -1)Q.push(tree[p.left]);if (p.right != -1)Q.push(tree[p.right]);}while (result.size() != 1){cout << result.front() << ' ';result.pop();}cout << result.front() << endl;}~Tree(){if (N)delete[] tree;}
};int main()
{int n = 0;cin >> n;Tree<unsigned> t1(n);t1.find_leaves();return 0;
}

03-树2 List Leaves相关推荐

  1. 算法 树2 List Leaves

    全部每周作业和视频思考题答案和解析 见 浙江大学 数据结构 思考题+每周练习答案汇总 题目:Given a tree, you are supposed to list all the leaves ...

  2. 广度优先遍历算法-03树的右侧问题

    树的右侧 简介 一个有点变形的二叉树的层序遍历. 问题描述 现在有一个果树,该树果子节点符合二叉树的分布,小王按照规定只能摘到从树的右侧看过去看到的第一个节点,求出节点序列. 问题分析 举例如下 假定 ...

  3. 深度解析(十五)哈夫曼树

    哈夫曼树(一)之 C语言详解 本章介绍哈夫曼树.和以往一样,本文会先对哈夫曼树的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理如出一辙 ...

  4. 数据结构JAVA实现——树

    数据结构JAVA实现--树 数据结构java实现--树的双亲表示法 数据结构Java实现--树|N叉树之孩子双亲表示法--顺序存储结构+链表 无序二叉树的实现 前序线索二叉树,中序线索二叉树 后续线索 ...

  5. 数据结构——哈夫曼(Huffman)树+哈夫曼编码

    哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,哈夫曼编码是可变字长编码(VLC)的一种.Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头 ...

  6. 哈夫曼树(一)基本概念与C语言实现

    本章介绍哈夫曼树.和以往一样,本文会先对哈夫曼树的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可.若 ...

  7. 哈夫曼树(二) - C++实现

    上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...

  8. 【转】基本概念:过拟合、修剪、假正、假负

    通常会把整个训练集分成两个部分:拿数据的约 60-80 % 放入我们的训练集,用来生成模型:然后拿剩下的数据放入一个测试集,在模型生成后,立即用其来测试我们模型的准确性. 那么这个额外的步骤为什么在此 ...

  9. 用 WEKA 进行数据挖掘,第 2 部分: 分类和群集

    from:http://www.ibm.com/developerworks/cn/opensource/os-weka2/index.html 简介 在 用 WEKA 进行数据挖掘,第 1 部分:简 ...

  10. 爱了!京东新产算法宝典在Github上爆火,成功激起了老夫的少女心!

    导言 算法是一门学问,但却总遭到一些程序员的冷落.现在的开发人员,更热衷于编程语言的修炼,以应付面试需求时的需要,所以对算法的学习,稍许忽略了些.实际上,近些年来,各互联网公司对于算法的要求也越来越高 ...

最新文章

  1. Odoo之Field
  2. mongodb 对内存的严重占用以及解决方法
  3. nginx负载均衡简单配置
  4. 关于面试,避开这几点,成功几率更大~~~
  5. 有些事情现在不做一辈子就都不会做了
  6. bzoj3214 [Zjoi2013]丽洁体 dp
  7. HTML5 代码要怎样凭“魅力”吸引搜索引擎的注意?
  8. 来自lombok的注解(解决idea中的找不到get,set方法,找不到log的问题)
  9. XShell——使用
  10. 获取context path或者basePath
  11. 总结定时器设计方法_PLC定时器(T)的工作原理及使用注意事项
  12. 数字图像处理-冈萨雷斯(学习笔记)
  13. Easy Touch 5 简单使用
  14. css bottom属性 使元素位置相对固定
  15. springboot框架下的实时消息推送
  16. 微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈
  17. FFMPEG入门资料---001---介绍和参数说明
  18. 1.Azure虚拟机部署
  19. Python数据分析——上海市二手房价格分析
  20. 【HBase】16-计数器

热门文章

  1. 网络图片异步加载(用到多线程(线程池),java回调机制,图片缓存,图片的动画)
  2. RPA手把手——写 Python 时要避免的十个错误
  3. 学校网页制作模板制作需要注意什么?
  4. Live-Server使用
  5. three.js 跳跳乐游戏
  6. 香港真缺人了!又一间与内地合作的联合实验室成立!
  7. Win10正式版 13个新特性新功能
  8. 高中选科学计算机可以不学物理吗,新高考为什么特别重视物理学科,高中生要不要选物理科目?...
  9. 孙溟㠭先生禅意篆刻——微笑
  10. Mysql深度讲解 – 子查询优化