Skip to content

Commit 5919f5e

Browse files
add a solution for 404
1 parent 82a298e commit 5919f5e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
58
public class _404 {
69
public static class Solution1 {
710
public int sumOfLeftLeaves(TreeNode root) {
@@ -51,4 +54,33 @@ public int sumOfLeftLeaves(TreeNode root) {
5154
return sum;
5255
}
5356
}
57+
58+
public static class Solution3 {
59+
/**
60+
* My completely original solution on 11/4/2021.
61+
*/
62+
public int sumOfLeftLeaves(TreeNode root) {
63+
Queue<TreeNode> queue = new LinkedList<>();
64+
queue.offer(root);
65+
int level = 0;
66+
int sum = 0;
67+
while (!queue.isEmpty()) {
68+
int size = queue.size();
69+
for (int i = 0; i < size; i++) {
70+
TreeNode curr = queue.poll();
71+
if (curr == null) {
72+
continue;
73+
}
74+
if (level > 0 && curr.left == null && curr.right == null && i % 2 != 1) {
75+
sum += curr.val;
76+
}
77+
queue.offer(curr.left);
78+
queue.offer(curr.right);
79+
80+
}
81+
level++;
82+
}
83+
return sum;
84+
}
85+
}
5486
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._404;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static junit.framework.TestCase.assertEquals;
12+
13+
public class _404Test {
14+
private static _404.Solution1 solution1;
15+
private static _404.Solution2 solution2;
16+
private static _404.Solution3 solution3;
17+
private static TreeNode root;
18+
private static int expected;
19+
20+
@BeforeClass
21+
public static void setup() {
22+
solution1 = new _404.Solution1();
23+
solution2 = new _404.Solution2();
24+
solution3 = new _404.Solution3();
25+
}
26+
27+
@Test
28+
public void test1() {
29+
root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7));
30+
expected = 24;
31+
assertEquals(expected, solution1.sumOfLeftLeaves(root));
32+
assertEquals(expected, solution2.sumOfLeftLeaves(root));
33+
assertEquals(expected, solution3.sumOfLeftLeaves(root));
34+
}
35+
36+
@Test
37+
public void test2() {
38+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 2));
39+
expected = 0;
40+
assertEquals(expected, solution1.sumOfLeftLeaves(root));
41+
assertEquals(expected, solution2.sumOfLeftLeaves(root));
42+
assertEquals(expected, solution3.sumOfLeftLeaves(root));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy