Skip to content

Commit 1ef5b18

Browse files
committed
maximum-depth-of-binary-tree
1 parent 36ed55b commit 1ef5b18

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
DFS를 이용하여, 최대 깊이를 구하는 방식
3+
트리 노드의 개수 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(log N)
6+
*/
7+
class Solution {
8+
public int maxDepth(TreeNode root) {
9+
return calculateDepth(root, 0);
10+
}
11+
12+
public int calculateDepth(TreeNode node, int depth) {
13+
if(node == null) {
14+
return depth;
15+
}
16+
17+
return Math.max(calculateDepth(node.left, depth + 1), calculateDepth(node.right, depth + 1));
18+
}
19+
}

0 commit comments

Comments
 (0)