Skip to content

Commit de2bc3b

Browse files
refactor 513
1 parent 03c013b commit de2bc3b

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

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

+21-17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.Queue;
77

88
/**
9+
* 513. Find Bottom Left Tree Value
10+
*
911
* Given a binary tree, find the leftmost value in the last row of the tree.
1012
1113
Example 1:
@@ -35,25 +37,27 @@
3537
*/
3638
public class _513 {
3739

38-
public int findBottomLeftValue(TreeNode root) {
39-
Queue<TreeNode> queue = new LinkedList<>();
40-
queue.offer(root);
41-
TreeNode leftMost = root;
42-
while (!queue.isEmpty()) {
43-
int size = queue.size();
44-
for (int i = 0; i < size; i++) {
45-
TreeNode curr = queue.poll();
46-
if (i == 0) {
47-
leftMost = curr;
48-
}
49-
if (curr.left != null) {
50-
queue.offer(curr.left);
51-
}
52-
if (curr.right != null) {
53-
queue.offer(curr.right);
40+
public static class Solution1 {
41+
public int findBottomLeftValue(TreeNode root) {
42+
Queue<TreeNode> queue = new LinkedList<>();
43+
queue.offer(root);
44+
TreeNode leftMost = root;
45+
while (!queue.isEmpty()) {
46+
int size = queue.size();
47+
for (int i = 0; i < size; i++) {
48+
TreeNode curr = queue.poll();
49+
if (i == 0) {
50+
leftMost = curr;
51+
}
52+
if (curr.left != null) {
53+
queue.offer(curr.left);
54+
}
55+
if (curr.right != null) {
56+
queue.offer(curr.right);
57+
}
5458
}
5559
}
60+
return leftMost.val;
5661
}
57-
return leftMost.val;
5862
}
5963
}

src/test/java/com/fishercoder/_513Test.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
* Created by fishercoder on 1/15/17.
1313
*/
1414
public class _513Test {
15-
private static _513 test;
15+
private static _513.Solution1 solution1;
1616
private static int expected;
1717
private static int actual;
1818
private static TreeNode root;
1919

2020
@BeforeClass
2121
public static void setup() {
22-
test = new _513();
22+
solution1 = new _513.Solution1();
2323
}
2424

2525
@Before
@@ -35,7 +35,7 @@ public void test1() {
3535
root.left = new TreeNode(1);
3636
root.right = new TreeNode(3);
3737
expected = 1;
38-
actual = test.findBottomLeftValue(root);
38+
actual = solution1.findBottomLeftValue(root);
3939
assertEquals(expected, actual);
4040

4141
}
@@ -50,7 +50,7 @@ public void test2() {
5050
root.right.right = new TreeNode(6);
5151
root.right.left.left = new TreeNode(7);
5252
expected = 7;
53-
actual = test.findBottomLeftValue(root);
53+
actual = solution1.findBottomLeftValue(root);
5454
assertEquals(expected, actual);
5555
}
5656
}

0 commit comments

Comments
 (0)