260120 : [BOJ 22856] 트리 순회 #2323
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}
🧩 문제 해결
스스로 해결: ✅
🔎 접근 과정
일단 DFS를 진행하여 중위순회의 끝을 구한다.
이후 DFS를 진행하되 "다시 부모로 돌아가는 길"을 생각하여 왼쪽 노드 / 오른쪽 노드를 stack에 삽입하면서 top도 같이 삽입해준다.
이렇게 하면 문제에서 원하는 유사 중위 순회가 완성이 된다.
종료 조건은 현재 노드를 기준으로 모든 자식 노드를 탐색하였으며, 중위 순회의 끝에 도달할 경우이다.
⏱️ 시간 복잡도
O(N)tree 순회는 노드의 개수 만큼 진행되기 때문이다.
💻 구현 코드