Skip to content

Commit 959c10c

Browse files
committed
feat: Maximum Depth of Binary Tree solution
1 parent 9ccd081 commit 959c10c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def maxDepth(self, root: Optional[TreeNode]) -> int:
3+
"""
4+
Intuition:
5+
์™ผ์ชฝ ์ž์‹๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹์— ๋Œ€ํ•ด depth๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ ์žฌ๊ท€ํ•˜๊ณ ,
6+
๋‘˜ ์ค‘ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
7+
8+
Time Complexity:
9+
O(N):
10+
๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•ด ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ O(N)์ด๋‹ค.
11+
12+
Space Complexity:
13+
O(1):
14+
answer ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)์ด๋‹ค.
15+
"""
16+
def get_depth(node, depth):
17+
if not node:
18+
return depth
19+
20+
left = get_depth(node.left, depth + 1)
21+
right = get_depth(node.right, depth + 1)
22+
return max(left, right)
23+
24+
answer = get_depth(root, 0)
25+
return answer

0 commit comments

Comments
ย (0)