Skip to content

Commit 21ad745

Browse files
committed
add solution for maximum depth of binary tree
1 parent 5b56357 commit 21ad745

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
import java.util.*;
17+
class Solution {
18+
private static int solve(TreeNode root, int depth){
19+
if (root == null){
20+
return depth;
21+
}
22+
23+
//root가null이 아닌 경우
24+
return Math.max(solve(root.left, depth+1) , solve(root.right, depth+1));
25+
26+
}
27+
public int maxDepth(TreeNode root) {
28+
int result = solve(root, 0);
29+
return result;
30+
}
31+
}
32+

0 commit comments

Comments
 (0)