pat 03-树2 List Leaves(mooc 陈越、何钦铭-数据结构)
来源:互联网 发布:稻谷出糙率计算数据 编辑:程序博客网 时间:2024/05/21 07:59
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 NNN (≤10\le 10≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1N-1N−1. Then NNN 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:
输出:
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer NNN (≤10\le 10≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1N-1N−1. Then NNN 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.
该题目是我在mooc上学习数据结构遇到的课后作业题,题目要求列出所有的叶子节点,并且顺序是从上到下,从左到右。其实就是一层一层的从左到右输出叶子节点。
输入:
81 -- -0 -2 7- -- -5 -4 6
输出:
4 1 5
1.解法
实际上就是一个层序遍历问题,在遍历过程中遇到叶子节点(两个儿子都是空)就输出该节点值。
from collections import deque #导入队列类deque#将读入的数据转化为数字,遇到'-'时返回-1def integer(a): if a=='-': return -1 else: return int(a)def build_tree(): n = input() lists = [] for m in xrange(n): line = raw_input().split() #读入一个节点#因为raw_input()读入的类型为字符串,需要将其转换为整数,-1代表空。 lists.append((integer(line[0]),integer(line[1]))) #储存一个节点 tag = [0 for m in xrange(n+1)] #tag[m]=0时说明该节点没有被其他节点指向,该节点为根节点。 for m in xrange(n): tag[lists[m][0]] = 1 tag[lists[m][1]] = 1#找出根节点 for m in xrange(n): if(tag[m]==0): break return (lists,m)def find(lists,m): #找到所有的叶子节点 a = deque(maxlen=len(lists)+1) #申请一个比该树节点树略大的队列。 a.append(m) #层序遍历 result = [] while(len(a)): m = a.popleft() if(m==-1): #空节点 continue if(lists[m][0]==-1 and lists[m][1]==-1): #叶子节点 result.append(m) else: a.append(lists[m][0]) #普通节点 a.append(lists[m][1]) result = [str(m) for m in result] result = ' '.join(result) #返回结果字符串 return result(lists,m) = build_tree()print find(lists,m) #输出结果
0 0
- pat 03-树2 List Leaves(mooc 陈越、何钦铭-数据结构)
- 中国大学MOOC-陈越、何钦铭-数据结构-2017秋 03-树2 List Leaves(25 point(s))
- 中国大学MOOC-陈越、何钦铭-数据结构 List Leaves
- MOOC浙大数据结构 — 03-树2 List Leaves (25分)
- PAT 数据结构 03-树2. List Leaves (25)
- PAT 03-树2 List Leaves(Java实现)
- 中国大学MOOC-陈越、何钦铭-数据结构-2017秋 03-树1 树的同构(25 point(s))
- 中国大学MOOC-陈越、何钦铭-数据结构-2017秋03-树3 Tree Traversals Again(25 point(s))
- PAT-中国大学MOOC-陈越、何钦铭-数据结构2015春习题集-00-自测2. 素数对猜想 (20)
- 中国大学MOOC-陈越、何钦铭-数据结构 File Transfer
- 中国大学MOOC-陈越、何钦铭-数据结构 统计工龄
- 中国大学MOOC-陈越、何钦铭-数据结构-2017春
- PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测5. Shuffling Machine (20) (简单模拟)【二星级】
- [MOOC-陈越何钦铭数据结构]02-1. Reversing Linked List (25)
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 03-树3 Tree Traversals Again (25分)
- 中国大学MOOC-陈越、何钦铭-数据结构 树的同构
- 中国大学MOOC-陈越、何钦铭-数据结构 是否同一棵二叉搜索树
- 中国大学MOOC-陈越、何钦铭-数据结构-2016秋2题
- Unity Shader 基础4--- CG 标准函数库
- hdu4714 树形dp
- Zookeeper--Apache ZooKeeper Watcher 机制源码解释
- Android中Context详解 ---- 你所不知道的Context
- vulakn教程--Drawing a Triangle--Draw--Framebuffer
- pat 03-树2 List Leaves(mooc 陈越、何钦铭-数据结构)
- oracle update set select from 关联更新
- leetcode 58. Length of Last Word
- 结巴分词流程图
- 实现矩阵的转置&原矩阵与转置后矩阵的乘机
- Point类的定义
- 【题解】 2016 ACM/ICPC Asia Regional Qingdao Online (6+5)
- JAVA 初学 i18n 国际化
- 第四周项目2-建设“单链表”算法库