Skip to content

Commit 80dd434

Browse files
author
jinvicky
committed
max depth solution
1 parent f1cdd1e commit 80dd434

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
// 최대 깊이를 탐색하는 것이므로 BFS보다 DFS가 더 적절합니다.
3+
// 매 노드의 left, right을 재귀로 호출해서 최대 깊이를 반환합니다. 재귀 함수 필요.
4+
public int maxDepth(TreeNode root) {
5+
// 재귀 종료 조건 == left 혹은 right가 null일 때, root 자체가 널이라면?
6+
if (root == null) return 0;
7+
8+
// 누적을 위해서 1+를 하고, Math.max()로 최댓값을 구합니다.
9+
return 1+ Math.max(maxDepth(root.left), maxDepth(root.right));
10+
}
11+
}

0 commit comments

Comments
 (0)