Skip to content

Commit f0245ab

Browse files
committed
104. Maximum Depth of Binary Tree (재귀 함수 풀이 추가)
1 parent 5723acb commit f0245ab

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

maximum-depth-of-binary-tree/okyungjin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,20 @@ def maxDepth(self, root: Optional[TreeNode]) -> int:
6262
depth += 1
6363

6464
return depth
65+
66+
'''
67+
재귀 함수 풀이
68+
69+
시간 복잡도: O(N)
70+
공간 복잡도: O(H), H: 스택의 높이
71+
'''
72+
class Solution:
73+
def maxDepth(self, root: Optional[TreeNode]) -> int:
74+
if root is None:
75+
return 0
76+
77+
left_depth = self.maxDepth(root.left)
78+
right_depth = self.maxDepth(root.right)
79+
80+
# 현재 자신의 노드 하나를 더해서 최대 깊이를 반환
81+
return max(left_depth, right_depth) + 1

0 commit comments

Comments
 (0)