Skip to content
Open
Show file tree
Hide file tree
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
56 changes: 56 additions & 0 deletions binary-tree-level-order-traversal/liza0525.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: BFS, DFS
  • 설명: 이 코드는 트리의 레벨별 순회를 위해 BFS와 DFS 방식을 모두 사용하며, 각각 큐와 재귀 호출을 활용하는 대표적인 트리 탐색 패턴입니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.levelOrder — Time: ✅ O(n) → O(n) / Space: ✅ O(w) → O(w)
유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(w) O(w)

피드백: 큐를 이용한 BFS 탐색으로 모든 노드를 한 번씩 방문하며, 최대 노드 수(w)에 비례하는 공간을 사용합니다.

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

풀이 2: Solution.levelOrder — Time: ✅ O(n) → O(n) / Space: ✅ O(h) → O(h)
유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(h) O(h)

피드백: 재귀 호출 스택이 트리의 높이(h)에 비례하며, 모든 노드를 한 번씩 방문합니다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 7기 풀이
class Solution:
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.

BFS와 DFS 두 가지 방식으로 레벨 순회를 모두 잘 구현해 주셔서, 트리 탐색 패턴을 비교하면서 학습하기 좋은 코드였어요 👍

# 풀이 1 - BFS
# 시간 복잡도: O(n)
# - 노드의 개수(n)만큼 모두 탐색하므로
# 공간 복잡도: O(w)
# - 최악은 한 레벨의 최대 노드 수(w)만큼 queue에 쌓임
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
res = []

nodes = deque()
if root:
nodes.appendleft(root)

while nodes:
childs = deque()
sibling_vals = []
while nodes:
node = nodes.pop()
if not node:
continue

sibling_vals.append(node.val)
if node.left:
childs.appendleft(node.left)
if node.right:
childs.appendleft(node.right)

res.append(sibling_vals)
nodes = childs

return res

# 풀이 2 - DFS
# 시간 복잡도: O(n)
# - 노드의 개수(n)만큼 모두 탐색하므로
# 공간 복잡도: O(h)
# - 재귀 스택이 최대 나무의 높이(h)만큼 쌓임
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
res = []

def dfs(node, depth):
if not node:
return

if len(res) == depth:
res.append([node.val])
else:
res[depth].append(node.val)

dfs(node.left, depth + 1)
dfs(node.right, depth + 1)

dfs(root, 0)

return res
16 changes: 16 additions & 0 deletions counting-bits/liza0525.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 이전 결과를 활용하여 현재 값을 계산하는 DP 방식을 사용하며, 최적의 중복 계산 방지와 효율성을 보여줍니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(n) O(n)

피드백: DP 배열을 이용하여 이전 결과를 활용하며, 모든 수에 대해 한 번씩 계산합니다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 7기 풀이
# 시간 복잡도: O(n)
# - n의 크기만큼 모든 수에 대한 dp 값을 찾음
# 공간 복잡도: O(n)
# - n의 크기만큼 dp 어레이를 사용
class Solution:
def countBits(self, n: int) -> List[int]:
dp = [0 for _ in range(n + 1)]

for i in range(1, n + 1):
if i % 2 == 1:
dp[i] = dp[i - 1] + 1
else:
dp[i] = dp[i // 2]

return dp
Loading