[dahyeong-yun] WEEK 10 Solutions - #2840
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
invert-binary-tree/dahyeong-yun.java
/**
* TC : O(n)
* - 모든 노드를 방문해야 함
* SC : O(n)
* - 콜 스택이 n까지 쌓일 수 있음
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return root;
// 다음에 다음이 있는 경우만 스왑해서 리턴
if(root.left != null || root.right != null) {
TreeNode temp = invertTree(root.left);
root.left = invertTree(root.right);
root.right = temp;
}
return root;
}
}- 패턴: Depth-First Search, Backtracking
- 설명: 트리의 모든 노드를 재귀적으로 방문하며 좌우를 교환하는 방식으로 트리를 뒤집는 DFS 구조이며, 각 노드의 처리 후 재귀 호출로 상태를 역전시키는 백트래킹 특성을 보인다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(n) | O(n) | ✅ |
| Space | O(n) | O(n) | ✅ |
피드백: 모든 노드를 방문하며, 최악의 경우 호출 스택이 트리 깊이만큼 쌓일 수 있다.
개선 제안: 현재 구현이 적절해 보입니다.
Contributor
📊 dahyeong-yun 님의 학습 현황이번 주 제출 문제
누적 학습 요약
문제 풀이 현황
🤖 이 댓글은 GitHub App을 통해 자동으로 작성되었습니다. 🔢 API 사용량 (gpt-5-nano)
|
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
search-in-rotated-sorted-array/dahyeong-yun.java
/**
* TC : O(log n)
* - 이진 탐색을 수행하기 때문에 log n
* SC : O(1)
* - 별도 유의미한 공간 할당이 없음
*/
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if(nums[mid] == target) {
return mid;
}
if (nums[left] <= nums[mid]) {
if(nums[left] <= target && target <= nums[mid])
right = mid;
else
left = mid + 1;
} else {
if (nums[mid] <= target && target <= nums[right])
left = mid;
else
right = mid - 1;
}
}
return -1;
}
}- 패턴: Binary Search, Monotonic Stack
- 설명: 배열이 회전되었더라도 이진 탐색으로 중간값과 경계 비교를 통해 타겟 위치를 탐색하므로 Binary Search 패턴에 속합니다. 회전으로 인한 구간 단순화는 맥락상 이진 탐색의 유효성을 유지하는_monotonic 부분(부분 배열의 정렬성 유지)으로 해석할 수 있습니다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(log n) | O(log n) | ✅ |
| Space | O(1) | O(1) | ✅ |
피드백: 회전된 배열에서도 이진 탐색의 성질을 유지하며 인덱스 범위를 조정한다.
개선 제안: 현재 구현이 적절해 보입니다.
Yiseull
self-requested a review
August 29, 2026 06:43
Yiseull
approved these changes
Aug 29, 2026
Comment on lines
+12
to
+16
| if(root.left != null || root.right != null) { | ||
| TreeNode temp = invertTree(root.left); | ||
| root.left = invertTree(root.right); | ||
| root.right = temp; | ||
| } |
Contributor
There was a problem hiding this comment.
두 자식이 모두 null일 때, 어차피 null끼리 스왑해도 결과가 같으니까, if(root.left != null || root.right != null) 조건문이 없는게 더 가독성 측면에서 좋을 것 같아요~!
Contributor
Author
There was a problem hiding this comment.
그렇네요. 편향트리에서 null 케이스가 나왔을 때 거기까지 생각이 미쳤어야 했네요. 코멘트 감사합니다 :)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!