CS/알고리즘_문제풀이(파이썬)

트리 순회 결과 출력하기

Jedy_Kim 2021. 5. 27. 15:49
728x90

문제

루트가 0인 이진트리가 주어질 때, 이를 전위순회, 중위순회, 후위순회한 결과를 각각 출력하는 프로그램을 작성하시오.

 

입력

첫 번째 줄에 트리의 노드 개수 n이 주어진다. ( 1 ≤ n ≤ 100 ) 두 번째 줄부터 트리의 정보가 주어진다. 각 줄은 3개의 숫자 a, b, c로 이루어지며, 그 의미는 노드 a의 왼쪽 자식노드가 b, 오른쪽 자식노드가 c라는 뜻이다. 자식노드가 존재하지 않을 경우에는 -1이 주어진다.

 

출력

첫 번째 줄에 전위순회, 두 번째 줄에 중위순회, 세 번째 줄에 후위순회를 한 결과를 출력한다.

 

예제 입력

6

0 1 2

1 3 4

2 -1 5

3 -1 -1

4 -1 -1

5 -1 -1

예제 출력

0 1 3 4 2 5

3 1 4 0 2 5

3 4 1 5 2 0

 

#코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import sys
 
class Tree:
  left  = [0 for _ in range(100)]
  right = [0 for _ in range(100)]
 
def preorder(tree, x): 
  # x를 루트로 하는 서브트리를 전위순회 하여 출력하는 함수
  # 기저조건
  if tree.left[x] == -1 and tree.right[x] == -1:
    print(x, end=' ')
  else:
    # Root -> left -> right (전 중 후)
    print(x, end=' ')
    if tree.left[x] != -1:
      preorder(tree, tree.left[x])
    if tree.right[x] != -1:
      preorder(tree, tree.right[x])
      
def inorder(tree, x):
  if tree.left[x] == -1 and tree.right[x] == -1:
    print(x, end=' ')
  else:
    if tree.left[x] != -1:
      inorder(tree, tree.left[x])
    print(x, end=' ')
    if tree.right[x] != -1:
      inorder(tree, tree.right[x]) 
    
def postorder(tree, x):
  if tree.left[x] == -1 and tree.right[x] == -1:
    print(x, end=' ')
  else:
    if tree.left[x] != -1:
      postorder(tree, tree.left[x])
    if tree.right[x] != -1:
      postorder(tree, tree.right[x]) 
    
    print(x, end=' ')
  
 
if __name__ == "__main__"
  n = int(sys.stdin.readline())
  getInfo = []
  tree = Tree()
  
  for i in range(n):
    a, b, c = map(int, sys.stdin.readline().split()) 
    tree.left[a] = b
    tree.right[a] = c  
  
  preorder(tree, 0)
  print()
  inorder(tree, 0)
  print()
  postorder(tree, 0)
cs

 

#복습

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
 
def preorder(x):
  # x를 루트로 하는 서브트리를 전위순회 하여 출력하는 함수
  # 기저조건 : 노드의 자식 노드가 없을 때
  if myTree[x][0== -1 and myTree[x][1== -1:
    print(x, end=' ')
  else:
    print(x, end=' ')
    if myTree[x][0!= -1:
      preorder(myTree[x][0])
    if myTree[x][1!= -1:
      preorder(myTree[x][1])
      
def inorder(x):
  if myTree[x][0== -1 and myTree[x][1== -1:
    print(x, end=' ')
  else:
    if myTree[x][0!= -1:
      inorder(myTree[x][0])
    print(x, end=' ')
    if myTree[x][1!= -1:
      inorder(myTree[x][1])
      
def postorder(x):
  # 왼쪽 - 오른쪽 - 중간
  if myTree[x][0== -1 and myTree[x][1== -1:
    print(x, end=' ')
  else:
    if myTree[x][0!= -1:
      postorder(myTree[x][0])
    if myTree[x][1!= -1:
      postorder(myTree[x][1])
    print(x, end=' ')
 
 
if __name__ == "__main__":
  input = sys.stdin.readline 
  n = int(input()) 
  myTree = [[] for _ in range(n)] 
  for _ in range(n):
    a, b, c = map(int, input().split()) 
    myTree[a].append(b)
    myTree[a].append(c)
   
  preorder(0)
  print()
  inorder(0)
  print()
  postorder(0)
cs

 

반응형

'CS > 알고리즘_문제풀이(파이썬)' 카테고리의 다른 글

거듭 제곱 구하기 L  (0) 2021.05.28
공통 조상 찾기  (0) 2021.05.27
원형큐 구현하기  (0) 2021.05.26
괄호의 값  (0) 2021.05.26
회전탑  (0) 2021.05.26