Skip to content

Commit 269516b

Browse files
committed
feat: fix Space Complexity for Maximum Depth of Binary Tree solution
1 parent 2e059f5 commit 269516b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

maximum-depth-of-binary-tree/thispath98.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ def maxDepth(self, root: Optional[TreeNode]) -> int:
1010
모든 노드에 대해 재귀적으로 호출하므로 O(N)이다.
1111
1212
Space Complexity:
13-
O(1):
14-
answer 변수만 사용하므로 O(1)이다.
13+
O(h):
14+
트리의 높이 h만큼 재귀 함수를 호출하므로,
15+
공간 복잡도는 O(h)이다.
1516
"""
17+
1618
def get_depth(node, depth):
1719
if not node:
1820
return depth
1921

2022
left = get_depth(node.left, depth + 1)
2123
right = get_depth(node.right, depth + 1)
2224
return max(left, right)
23-
25+
2426
answer = get_depth(root, 0)
2527
return answer

0 commit comments

Comments
 (0)