pat 03-树2 List Leaves(mooc 陈越、何钦铭-数据结构)
2016-09-19 20:48
597 查看
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:
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上学习数据结构遇到的课后作业题,题目要求列出所有的叶子节点,并且顺序是从上到下,从左到右。其实就是一层一层的从左到右输出叶子节点。
输入:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
输出:
4 1 5
1.解法
实际上就是一个层序遍历问题,在遍历过程中遇到叶子节点(两个儿子都是空)就输出该节点值。
from collections import deque #导入队列类deque
#将读入的数据转化为数字,遇到'-'时返回-1
def 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) #
4000
层序遍历
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) #输出结果
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上学习数据结构遇到的课后作业题,题目要求列出所有的叶子节点,并且顺序是从上到下,从左到右。其实就是一层一层的从左到右输出叶子节点。
输入:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
输出:
4 1 5
1.解法
实际上就是一个层序遍历问题,在遍历过程中遇到叶子节点(两个儿子都是空)就输出该节点值。
from collections import deque #导入队列类deque
#将读入的数据转化为数字,遇到'-'时返回-1
def 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) #
4000
层序遍历
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) #输出结果
相关文章推荐
- 中国大学MOOC-陈越、何钦铭-数据结构-2017秋 03-树1 树的同构(25 point(s))
- PAT 数据结构 03-树2. List Leaves (25)
- 中国大学MOOC-陈越、何钦铭-数据结构-2017秋 03-树2 List Leaves(25 point(s))
- 中国大学MOOC-陈越、何钦铭-数据结构-2017秋03-树3 Tree Traversals Again(25 point(s))
- PTA中国大学MOOC-陈越、何钦铭-数据结构-2017春课后作业03树1 树的同构
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 03-树3 Tree Traversals Again (25分)
- 中国大学MOOC-陈越、何钦铭-数据结构 堆中的路径
- 中国大学MOOC-陈越、何钦铭-数据结构-笔记
- 中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题&代码
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
- 中国大学MOOC-陈越、何钦铭-数据结构-2017春
- 03-树1 树的同构(25 分) 2017秋 数据结构 陈越、何钦铭
- 中国大学MOOC-陈越、何钦铭-数据结构-2017春期中考试
- PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测4. Have Fun with Numbers (20) 【二星级】
- PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测5. Shuffling Machine (20) (简单模拟)【二星级】
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋02-线性结构1题解
- 中国大学MOOC-陈越、何钦铭-数据结构 Maximum Subsequence Sum
- PAT-中国大学MOOC-陈越、何钦铭-数据结构2015春习题集-00-自测2. 素数对猜想 (20)
- PAT 03-2. List Leaves (25)
- 中国大学MOOC-陈越、何钦铭-数据结构 Insert or Merge