Skip to content

Commit 9984366

Browse files
author
sejineer
committed
maximum-depth-of-binary-tree solution
1 parent caa6e7c commit 9984366

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
시간 복잡도: O(N)
3+
공간 복잡도: O(h) h = 트리 높이
4+
"""
5+
class Solution:
6+
def maxDepth(self, root: Optional[TreeNode]) -> int:
7+
result = 0
8+
9+
def dfs(tree: Optional[TreeNode], depth: int):
10+
nonlocal result
11+
if tree == None:
12+
result = max(result, depth)
13+
return
14+
15+
dfs(tree.left, depth + 1)
16+
dfs(tree.right, depth + 1)
17+
18+
dfs(root, 0)
19+
return result

0 commit comments

Comments
 (0)