260120 : [BOJ 22856] 트리 순회 #2321
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.
🚀 이슈 번호
Resolve: {#2319}
🧩 문제 해결
스스로 해결: ✅ / ❌
🔎 접근 과정
중위순회의 마지막 노드(= 가장 오른쪽 노드)를 찾음
재귀를 돌면서 트리를 탐색하는데, 재귀를 호출했을때(자식으로 이동), 그리고 빠져나왔을 때(부모로 이동) 모두 count를 해주다가, 마지막 노드에 도달하면 멈춘다.
+) 더 최적화 시키려면 재귀 대신 전체 노드 개수 기반으로 count를 계산해 구할 수 있다.
⏱️ 시간 복잡도
O(N)재귀: N번당 오른쪽 왼쪽 탐색 OR 트리 모든 간선을 2번씩 방문(왕복)
마지막 노드 찾기: 최적의 경우 logN이지만, 최악의 경우 N
💻 구현 코드