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
21 changes: 21 additions & 0 deletions invert-binary-tree/sangbeenmoon.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(n)

피드백: 모든 노드를 한 번씩 방문하는 재귀 호출로 구현되어 있으며, 각 호출은 새로운 노드 객체를 생성하므로 시간과 공간 모두 노드 수에 비례한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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 root


def go(cur: TreeNode) -> Optional[TreeNode]:
if not cur:
return cur
res = TreeNode(cur.val)
res.left = go(cur.right)
res.right = go(cur.left)
return res

return go(root)
Loading