Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions invert-binary-tree/jamiebase.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 재귀를 이용하여 트리의 노드를 방문하며 왼쪽과 오른쪽 자식을 교체하는 방식으로 동작합니다. 따라서 깊이 우선 탐색(DFS) 패턴에 속합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(h)

피드백: 모든 노드를 한 번씩 방문하는 재귀적 트리 순회로 구현되어 있으며, 각 노드에서 교체 작업이 수행됩니다. 공간 복잡도는 재귀 호출 스택의 최대 높이인 트리 높이에 비례합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
# Approach
노드를 방문하면서 왼쪽 자식, 오른쪽 자식을 서로 교체합니다.
이 과정을 왼쪽/오른쪽 서브트리에 재귀적으로 반복합니다.

# Complexity
- Time complexity: 모든 노드를 다 방문해야 하기 때문에 O(N)
- Space complexity: 재귀 호출 스택이 트리 높이만큼 쌓일 수 있기 때문에 O(H)
"""


class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None

root.left, root.right = root.right, root.left
self.invertTree(root.left)
self.invertTree(root.right)
return root
Loading