8000 refactor 104 · yochju/Leetcode@0791941 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 0791941

Browse files
refactor 104
1 parent f8728b1 commit 0791941

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/main/java/com/fishercoder/solutions/_104.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44

55
/**
66
* 104. Maximum Depth of Binary Tree
7+
*
78
* Given a binary tree, find its maximum depth.
89
* The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
910
*/
1011
public class _104 {
1112

13+
public static class Solution1 {
1214
public int maxDepth(TreeNode root) {
13-
if (root == null) {
14-
return 0;
15-
}
16-
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
15+
if (root == null) {
16+
return 0;
17+
}
18+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
1719
}
20+
}
1821

1922
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._104;
6+
import java.util.Arrays;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _104Test {
13+
private static _104.Solution1 solution1;
14+
private static TreeNode root;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _104.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7));
24+
assertEquals(3, solution1.maxDepth(root));
25+
}
26+
}

0 commit comments

Comments
 (0)
0