[20250722] BOJ / P5 / 이진 검색 트리 / 권혁준 #521
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/1539
🧭 풀이 시간
20분
👀 체감 난이도
✏️ 문제 설명
0부터 N-1까지의 수가 한 번씩 등장하는 수열이 주어진다.
이 수열로 이진 검색 트리를 만들었을 때, 모든 점의 높이 합을 구해보자.
🔍 풀이 방법
이진 검색 트리에 어떤 수 a가 추가되는 경우는 두 가지로 나눌 수 있다.
a가 어떤 수의 왼쪽 자식이 되는 경우
-> 지금까지 트리에 들어간 수들 중 a보다 크면서 가장 작은 수의 왼쪽 자식으로 붙게 된다.
a가 어떤 수의 오른쪽 자식이 되는 경우
-> 마찬가지로 지금까지 트리에 들어간 수들 중 a보다 작으면서 가장 큰 수의 오른쪽 자식으로 붙게 된다.
TreeSet으로 정렬된 상태에서 a의 양 옆 수를 체크하고, 그 중 더 늦게 들어간 수한테 붙게 된다.
각 점의 높이와 나온 순서를 배열로 관리해서 해결했다.
⏳ 회고
상쾌한 아침