Skip to content

Commit a5875bc

Browse files
committed
Add solution for Maximum Depth of Binary Tree problem
1 parent 33dc6e9 commit a5875bc

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
# Intuition
3+
1) BFS + 큐
4+
2) DFS + 재귀
5+
1번으로 풀었다가 2번이 코드가 더 간결해서 바꿨습니다.
6+
7+
# Complexity
8+
- Time complexity: 모든 노드를 다 봐야 하므로 O(N)
9+
10+
- Space complexity: 재귀 깊이만큼 O(H)
11+
"""
12+
13+
class Solution:
14+
def maxDepth(self, root: Optional[TreeNode]) -> int:
15+
if not root:
16+
return 0
17+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
18+
19+

0 commit comments

Comments
 (0)