Skip to content

Commit ec3553f

Browse files
committed
FEAT : add solution (maximum-depth-of-binary-tree)
1 parent 3887c3b commit ec3553f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
TC: O(n)
3+
SC: O(n)
4+
'''
5+
6+
class Solution:
7+
def maxDepth(self, root: Optional[TreeNode]) -> int:
8+
# 재귀함수 호출 시 노드가 존재하지 않을 때 탈출한다
9+
if not root:
10+
return 0
11+
# root를 기준으로 왼쪽 서브트리, 오른쪽 서브트리를 재귀호출로 쌓아놓고 리프노드부터 세면서 트리의 깊이를 구한다
12+
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

0 commit comments

Comments
 (0)