博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
树 List Leaves 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】...
阅读量:4879 次
发布时间:2019-06-11

本文共 1413 字,大约阅读时间需要 4 分钟。

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

Sample Input:

81 -- -0 -2 7- -- -5 -4 6

Sample Output:

4 1 5 分析:数据量很小,怎么写都过啊。于是我用结构体数组来模拟建立树状结构。然后找到每个叶子节点,但输出有要求。先输出深度小的节点,深度相同的叶子节点 先输出靠左的叶子节点,再输出靠右的叶子节点。 样例建树后的样子
#include 
#include
#include
#include
#include
#include
using namespace std;struct node{ int ll; int rr; int data; int dep; int dfn;}q[20];struct N{ int num; int dep; int dfn; bool operator<(const N &dd)const{ if(dep==dd.dep) return dd.dfn
que; N cur; for(i=0; i

 

 

转载于:https://www.cnblogs.com/yspworld/p/4813696.html

你可能感兴趣的文章
进入保护模式完整代码(汇编)
查看>>
Add Font Awesome Icons to our Buttons
查看>>
汇编语言程序设计读书笔记(4)- 程序设计基础之一
查看>>
JSON.stringify语法解析(自己留存)
查看>>
Unity 动画系统 Animation和Animator等常用类
查看>>
Spring Boot 传参方式
查看>>
Copy a Table Included Data
查看>>
结构体指针做函数参数
查看>>
AppStore加急审核申请流程
查看>>
android之利用SQLite数据库实现登陆和注册
查看>>
mac保存远程链接
查看>>
eclipse对离线python的环境搭建
查看>>
实践小节
查看>>
netstate 参数
查看>>
某考试T1 game
查看>>
c# 播放音乐
查看>>
mysql备份数据库
查看>>
28-Ubuntu-远程管理命令-02-查看网卡的配置信息
查看>>
sublime text3---Emmet:HTML/CSS代码快速编写神器
查看>>
Android:AysncTask异步加载
查看>>