Skip to content

Commit 269516b

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

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

โ€Ž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)