Skip to content

Commit dcfb736

Browse files
committed
solve: Maximum Depth of Binary Tree
1 parent 046ac88 commit dcfb736

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
"""
22
Constraints:
3-
-
3+
- The number of nodes in the tree is in the range [0, 10^4].
4+
- -100 <= Node.val <= 100
45
5-
Time Complexity:
6-
-
6+
Time Complexity: O(N)
7+
- N์€ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์ˆ˜
8+
- ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๊ธฐ ๋•Œ๋ฌธ
79
8-
Space Complexity:
9-
-
10+
Space Complexity: O(H)
11+
- H๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด
12+
- ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ์ธํ•œ ํ˜ธ์ถœ ์Šคํƒ์˜ ์ตœ๋Œ€ ๊นŠ์ด๊ฐ€ ํŠธ๋ฆฌ์˜ ๋†’์ด์™€ ๊ฐ™์Œ
1013
1114
ํ’€์ด๋ฐฉ๋ฒ•:
12-
1.
15+
1. Base case: root๊ฐ€ None์ธ ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜
16+
2. ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ๊นŠ์ด๋ฅผ ๊ฐ๊ฐ ๊ณ„์‚ฐ
17+
3. ๋‘ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๊นŠ์ด ์ค‘ ์ตœ๋Œ€๊ฐ’์— 1์„ ๋”ํ•ด์„œ ๋ฐ˜ํ™˜ (ํ˜„์žฌ ๋…ธ๋“œ์˜ ๊นŠ์ด๋ฅผ ํฌํ•จ)
1318
"""
19+
# Definition for a binary tree node.
20+
# class TreeNode:
21+
# def __init__(self, val=0, left=None, right=None):
22+
# self.val = val
23+
# self.left = left
24+
# self.right = right
25+
class Solution:
26+
def maxDepth(self, root: Optional[TreeNode]) -> int:
27+
if root is None:
28+
return 0
29+
30+
left_depth = self.maxDepth(root.left)
31+
right_depth = self.maxDepth(root.right)
1432

33+
return max(left_depth, right_depth) + 1

0 commit comments

Comments
ย (0)