Skip to content

Commit e2b38da

Browse files
committed
#227 maximum-depth-of-binary-tree
1 parent 4634265 commit e2b38da

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
ํ’€์ด:
3+
์žฌ๊ท€ํ•จ์ˆ˜์˜ ํƒˆ์ถœ์กฐ๊ฑด์€ root๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ, return 0
4+
๊ทธ ์™ธ์—๋Š” left, right์— ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋” ๋†’์€ ๊ฐ’ + 1์„ return ํ•œ๋‹ค
5+
๊ฐ€์žฅ ๊นŠ์€ ๊นŠ์ด์˜ maxDepth๊ฐ€ 0์„ return ํ•˜๊ณ  ๋ฐ‘์—์„œ๋ถ€ํ„ฐ + 1 ์”ฉ ์Œ“์—ฌ์„œ ์ด ๊นŠ์ด๋ฅผ return ํ•  ์ˆ˜ ์žˆ๋‹ค
6+
7+
๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜: N
8+
9+
SC : O(N)
10+
11+
๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•ด ์ˆœํšŒ
12+
13+
TC : O(N)
14+
15+
๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•œ ์žฌ๊ท€์ฝœ์Šคํƒ
16+
17+
"""
18+
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 not root:
28+
return 0
29+
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

0 commit comments

Comments
ย (0)