Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 900 Bytes

1991.md

File metadata and controls

47 lines (35 loc) · 900 Bytes

백준 1991번 트리 순회

1991


소스코드

  • 메모리 : 29200 KB
  • 시간 : 68 ms
def preorder(root):
    if root != '.':
        print(root, end='')
        preorder(tree[root][0])
        preorder(tree[root][1])
 
 
def inorder(root):
    if root != '.':
        inorder(tree[root][0])
        print(root, end='')
        inorder(tree[root][1])
 
 
def postorder(root):
    if root != '.':
        postorder(tree[root][0])
        postorder(tree[root][1])
        print(root, end='')
        
        
        
n = int(input())
tree = {}
 
for i in range(n):
    root, left, right = input().split()
    tree[root] = [left, right]
 
preorder('A')
print()
inorder('A')
print()
postorder('A')