We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0899a76 commit 9dc43f4Copy full SHA for 9dc43f4
maximum-depth-of-binary-tree/eunhwa99.java
@@ -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