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 f1cdd1e commit 80dd434Copy full SHA for 80dd434
maximum-depth-of-binary-tree/jinvicky.java
@@ -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