문제) 백준 - 트리 - 트리의 높이와 너비
https://www.acmicpc.net/problem/2250
2250번: 트리의 높이와 너비
첫째 줄에 노드의 개수를 나타내는 정수 N(1 ≤ N ≤ 10,000)이 주어진다. 다음 N개의 줄에는 각 줄마다 노드 번호와 해당 노드의 왼쪽 자식 노드와 오른쪽 자식 노드의 번호가 순서대로 주어진다.
www.acmicpc.net
트리의 중위 선회를 통해 해방 레벨에서의 최소 idx와 최대 idx를 memo 합니다. 그 후, 1부터 N까지 너비의 최댓값을 찾아 출력합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void recursion(int now, int lev) { | |
if(graph[now][0] != -1) | |
recursion(graph[now][0], lev + 1); | |
l[lev] = min(l[lev], idx); | |
r[lev] = max(r[lev], idx++); | |
if (graph[now][1] != -1) | |
recursion(graph[now][1], lev + 1); | |
} |
Full Code)
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 14889번 - 스타트와 링크 (Python) 문제 및 풀이 (0) | 2022.04.03 |
---|---|
[백준] 13302번 - 리조트 (C++) 문제 및 풀이 (0) | 2022.03.18 |
[백준] 2075번 - N번째 큰 수 (C++) 문제 및 풀이 (0) | 2022.03.17 |
[백준] 9935번 - 문자열 폭발 (C++) 문제 및 풀이 (0) | 2022.03.17 |
[백준] 1613번 - 역사 (C++) 문제 및 풀이 (0) | 2022.03.16 |
댓글