数据结构3.2-List Leaves

问题

数据结构3.2-List Leaves_第1张图片
List Leaves

代码

n = int(input())

root = []
for i in range(n):
    root += [i]
    
tree = {}
for i in range(n):
    node = input().split()
    tree[i] = node
    
    if node[0] != '-':
        x = int(node[0])
        x = root.index(x)
        root.pop(x)
    
    if node[1] != '-':
        x = int(node[1])
        x = root.index(x)
        root.pop(x)

root = tree[ root[0] ] + root
    
queue = [root]
while(len(queue) > 0):
    parent = queue.pop(0)
    
    i = parent.pop()
    if parent == ['-', '-']:
        e = ' '
        if len(queue) == 0:
            e = ''
        print(i, end = e)
        continue
    
    if parent[0] != '-':
        left = int(parent[0])
        queue.append( tree[left] + [left])
        
    if parent[1] != '-':
        right = int(parent[1])
        queue.append( tree[right] + [right])

有任何问题请回复提出。然后欢迎关注微信公众号格物致愚

数据结构3.2-List Leaves_第2张图片
格物致愚

你可能感兴趣的:(数据结构3.2-List Leaves)