Skip to content

Commit 9dc43f4

Browse files
committed
maximum depth of binary tree
1 parent 0899a76 commit 9dc43f4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class eunhwa99 {
2+
3+
// 시간 복잡도: O(n) - 트리를 한 번 순회
4+
// 공간 복잡도: O(h) - 재귀 호출 스택 공간 (h는 트리의 높이)
5+
class Solution {
6+
public int maxDepth(TreeNode root) {
7+
if (root == null) return 0; // 빈 노드인 경우 깊이는 0
8+
int leftDepth = maxDepth(root.left); // 왼쪽 서브트리 깊이
9+
int rightDepth = maxDepth(root.right); // 오른쪽 서브트리 깊이
10+
return Math.max(leftDepth, rightDepth) + 1; // 최대 깊이 + 1 (현재 노드)
11+
}
12+
}
13+
}
14+

0 commit comments

Comments
 (0)