[Tree] 이진트리 재귀 순회 함수 만들기
순회하는 것 자체가 재귀이므로, 재귀 함수로 만들면 구현하는 과정을 이해하기 쉽다. 또한 순서만 조금씩 바꾸면 바로 중위 순회와 후위 순회로 변경할 수 있다. 전위 순회 함수 만들기 tree = ["A", "B", "C", "D", "E", "F", None, "G"] n = len(tree) def preorder(tree, i): if i < len(tree): print(tree[i], end=" ") # 방문 처리(출력) left, right = 2*i + 1, 2*i + 2 if left < len(tree) and tree[left] is not None: preorder(tree,left) if right < len(tree) and tree[right] is not None: preorde..
2023. 9. 7.
[백준 20057번] 마법사 상어와 토네이도
20057번: 마법사 상어와 토네이도 마법사 상어가 토네이도를 배웠고, 오늘은 토네이도를 크기가 N×N인 격자로 나누어진 모래밭에서 연습하려고 한다. 위치 (r, c)는 격자의 r행 c열을 의미하고, A[r][c]는 (r, c)에 있는 모래의 양을 www.acmicpc.net n = int(input()) area = [list(map(int,input().split())) for _ in range(n)] dx = [0,1,0,-1] dy = [-1,0,1,0] count = 0 length = 1 direction = 0 answer = 0 x, y = n//2, n//2 def move(x,y,d): global answer total = area[x][y] five_percent = int(tot..
2023. 9. 7.