[yuseok89] WEEK 10 Solutions - #2836
Open
yuseok89 wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
invert-binary-tree/yuseok89.py
# TC: O(N)
# SC: O(N)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
else:
cur_node = TreeNode(root.val)
cur_node.right = self.invertTree(root.left)
cur_node.left = self.invertTree(root.right)
return cur_node
- 패턴: Binary Search, Depth-First Search, Divide and Conquer
- 설명: 이 코드는 이진트리의 자식 노드를 서로 바꿔 트리를 반전시키는 재귀적 분할 처리로서, DFS 방식으로 각 서브트리를 처리하며 분할해서 해결하는 패턴을 사용합니다.
📊 시간/공간 복잡도 분석
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(N) | O(n) | ✅ |
| Space | O(N) | O(h) | ❌ |
피드백: 트리의 각 노드를 한 번씩 방문하여 자식 정보를 교환하므로 시간 복잡도는 O(n)이고, 재귀 호출 스택의 깊이에 비례하는 공간 복잡도 O(h)이다.
개선 제안: 현재 구현이 적절해 보입니다.
Contributor
📊 yuseok89 님의 학습 현황이번 주 제출 문제
누적 학습 요약
문제 풀이 현황
🤖 이 댓글은 GitHub App을 통해 자동으로 작성되었습니다. 🔢 API 사용량 (gpt-5-nano)
|
Contributor
There was a problem hiding this comment.
앞의 if가 이미 리턴을 하고 있어서 else 를 크게 쓰지 않으셔도 되지 않을까? 라는 생각이 듭니다!
또한 cur_node 를 만들지 않으시고 root를 직접 바꾸셔도 정답처리 되실거에요!
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 하나 이상을 반드시 검토를 해주셔야 합니다!