File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
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
You canโt perform that action at this time.
0 commit comments