Skip to content

Commit 166b93c

Browse files
committed
maximum-depth-of-binary-tree solution
1 parent 9ef6846 commit 166b93c

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public int maxDepth(TreeNode root) {
18+
/**
19+
문제: 바이너리트리의 maximum depth 구하기
20+
조건
21+
- 노드의 개수는 0 ~ 10^4
22+
- node val 은 -100 ~ 100
23+
풀이: dfs 로 depth 구하기
24+
- 주어진 트리는 binary tree
25+
- time complexity : O(n) , O(log n)
26+
- space complexity : O(h)
27+
*/
28+
29+
30+
if(root == null) return 0;
31+
32+
int height = dfs(root);
33+
return height;
34+
}
35+
36+
int dfs(TreeNode node) {
37+
if(node == null) return 0;
38+
39+
int left = dfs(node.left);
40+
int right = dfs(node.right);
41+
42+
return Math.max(left, right) + 1;
43+
}
44+
}

0 commit comments

Comments
 (0)