트리가 활용이 참 어려운것 같다.
def calc_node(start_node):
global node_cnt
if tree[start_node][1]:
node_cnt += 1 # 왼쪽 자식노드가 있을 떄 하나 추가
calc_node(tree[start_node][1]) # w자식노드로 가서 재귀
if tree[start_node][2]:
node_cnt += 1 #오른쪽 자식노드가 있을 때 하나 추가
calc_node(tree[start_node][2]) # w자식노드로 가서 재귀
t= int(input())
for tc in range(1,t+1):
E, N = map(int,input().split())
V = E + 1
adjl = list(map(int,input().split()))
tree = [[0] *3 for _ in range(V+1)]
for i in range(E):
parent, child = adjl[i*2], adjl[i*2+1]
tree[child][0] = parent # 0 에 부모
if tree[parent][1]:
tree[parent][2] = child
else:
tree[parent][1] = child
node_cnt = 1
calc_node(N)
print(f'#{tc} {node_cnt}')
'알고리즘 문제풀이' 카테고리의 다른 글
백준 1620. 나는야 포켓몬 마스터 이다솜 Python 실버4 (1) | 2024.02.21 |
---|---|
baekjoon No Duplicates Bronze 1 (0) | 2024.02.21 |
D5_1248. 공통조상 Python Tree 트리 (0) | 2024.02.20 |
Swea 1231. 중위순회 Python Tree (0) | 2024.02.20 |
백준 10953 A+B - 6 브론즈3 (0) | 2024.02.20 |