We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2e059f5 commit 269516bCopy full SHA for 269516b
1 file changed
โmaximum-depth-of-binary-tree/thispath98.pyโ
@@ -10,16 +10,18 @@ def maxDepth(self, root: Optional[TreeNode]) -> int:
10
๋ชจ๋ ๋ ธ๋์ ๋ํด ์ฌ๊ท์ ์ผ๋ก ํธ์ถํ๋ฏ๋ก O(N)์ด๋ค.
11
12
Space Complexity:
13
- O(1):
14
- answer ๋ณ์๋ง ์ฌ์ฉํ๋ฏ๋ก O(1)์ด๋ค.
+ O(h):
+ ํธ๋ฆฌ์ ๋์ด h๋งํผ ์ฌ๊ท ํจ์๋ฅผ ํธ์ถํ๋ฏ๋ก,
15
+ ๊ณต๊ฐ ๋ณต์ก๋๋ O(h)์ด๋ค.
16
"""
17
+
18
def get_depth(node, depth):
19
if not node:
20
return depth
21
22
left = get_depth(node.left, depth + 1)
23
right = get_depth(node.right, depth + 1)
24
return max(left, right)
-
25
26
answer = get_depth(root, 0)
27
return answer
0 commit comments