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