Skip to content

Commit 5601201

Browse files
committed
Add the serial solutions for 'Path Sum'
1 parent 0b4ffb5 commit 5601201

10 files changed

+342
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ setTargetCompatibility(JavaVersion.VERSION_1_7)
2424
dependencies {
2525
// The production code uses the SLF4J logging API at compile time
2626
compile 'org.slf4j:slf4j-api:1.7.7'
27+
compile 'com.google.guava:guava:18.0'
2728

2829
// Declare the dependency for your favourite test framework you want to use in your tests.
2930
// TestNG is also supported by the Gradle Test task. Just change the
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.sean.linkedlist;
2+
3+
public class ListNode<T> {
4+
public T value;
5+
public ListNode<T> next;
6+
7+
public ListNode(T value) {
8+
this.value = value;
9+
}
10+
11+
public ListNode(T value, ListNode<T> next) {
12+
this.value = value;
13+
this.next = next;
14+
}
15+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.sean.recursive;
2+
3+
import org.sean.tree.TreeNode;
4+
5+
import java.util.HashSet;
6+
7+
/***
8+
* 437. Path Sum III
9+
*/
10+
public class PathSumCounter {
11+
private int counter;
12+
private TreeNode top;
13+
14+
private HashSet<TreeNode> set = new HashSet<TreeNode>();
15+
private HashSet<TreeNode> selfTravelled = new HashSet<>();
16+
private void travelTree(TreeNode node, int sum, int currSum) {
17+
if(node != null) {
18+
int value = node.val;
19+
if(value == sum) {
20+
if(!set.contains(node)) {
21+
set.add(node);
22+
}
23+
}
24+
25+
int newSum = currSum + value;
26+
27+
// middle nodes itself traversal
28+
if(node != top) {
29+
// extend from parent
30+
if(newSum == sum) {
31+
++counter;
32+
}
33+
34+
// check duplicate node traversal
35+
if(!selfTravelled.contains(node)) {
36+
travelTree(node.left, sum, value);
37+
travelTree(node.right, sum, value);
38+
39+
selfTravelled.add(node);
40+
}
41+
}
42+
43+
// normal traversal with added up result
44+
travelTree(node.left, sum, newSum);
45+
travelTree(node.right, sum, newSum);
46+
}
47+
}
48+
49+
public int pathSum(TreeNode root, int sum) {
50+
counter = 0;
51+
52+
if(root != null) {
53+
top = root;
54+
set.clear();
55+
56+
travelTree(root, sum, 0);
57+
}
58+
59+
return counter + set.size();
60+
}
61+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.sean.recursive;
2+
3+
import org.sean.tree.TreeNode;
4+
5+
import java.util.LinkedList;
6+
7+
/***
8+
* 129. Sum Root to Leaf Numbers
9+
*/
10+
public class RootLeafPathCalculator {
11+
LinkedList<Integer> path = new LinkedList<>();
12+
13+
private int sum = 0;
14+
private boolean addNode(TreeNode root) {
15+
if(root != null) {
16+
path.add(root.val);
17+
18+
if(root.left == null && root.right == null) {
19+
int result = 0;
20+
int len = path.size();
21+
for(int i = 0; i < len; i++) {
22+
int digit = path.get(i);
23+
result += digit * Math.pow(10, (len -1 -i));
24+
}
25+
26+
sum+= result;
27+
}
28+
29+
boolean leftAdded = addNode(root.left);
30+
if(leftAdded) {
31+
path.removeLast();
32+
}
33+
34+
boolean rightAdded = addNode(root.right);
35+
if(rightAdded) {
36+
path.removeLast();
37+
}
38+
39+
return true;
40+
}
41+
return false;
42+
}
43+
44+
public int sumNumbers(TreeNode root) {
45+
if(root == null)
46+
return 0;
47+
48+
if(root.left == null && root.right == null) {
49+
return root.val;
50+
}
51+
52+
addNode(root);
53+
54+
return sum;
55+
56+
}
57+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.sean.recursive;
2+
3+
import org.sean.tree.TreeNode;
4+
5+
/***
6+
* 112. Path Sum
7+
*/
8+
public class RootLeafPathFinder {
9+
private TreeNode top;
10+
private boolean findPathSum(TreeNode node, int sum, int curr) {
11+
12+
int value = curr + node.val;
13+
if(node.left == null && node.right == null) {
14+
return (value == sum);
15+
}else {
16+
boolean leftFound = false;
17+
if(node.left != null) {
18+
leftFound = findPathSum(node.left, sum, value);
19+
}
20+
21+
boolean rightFound = false;
22+
if(node.right != null) {
23+
rightFound = findPathSum(node.right, sum, value);
24+
}
25+
26+
return leftFound || rightFound;
27+
}
28+
}
29+
30+
public boolean hasPathSum(TreeNode root, int sum) {
31+
if(root == null)
32+
return false;
33+
34+
int s = root.val;
35+
if(root.left == null && root.right == null) {
36+
return s == sum;
37+
}else {
38+
top = root;
39+
return findPathSum(root, sum, 0);
40+
}
41+
}
42+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.sean.recursive;
2+
3+
import org.sean.tree.TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
/***
9+
* 113. Path Sum II
10+
*/
11+
public class RootLeafSumPathFinder {
12+
List<List<Integer>> result = new LinkedList<List<Integer>>();
13+
LinkedList<Integer> path = new LinkedList<>();
14+
15+
private int sum;
16+
17+
private List<Integer> copy(List<Integer> path) {
18+
List<Integer> list = new LinkedList<>();
19+
list.addAll(path);
20+
return list;
21+
}
22+
23+
private boolean addNode(TreeNode root) {
24+
if (root != null) {
25+
path.add(root.val);
26+
27+
if (root.left == null && root.right == null) {
28+
29+
int total = 0;
30+
int len = path.size();
31+
for (int i = 0; i < len; i++) {
32+
int digit = path.get(i);
33+
total += digit;
34+
}
35+
36+
if (total == sum) {
37+
result.add(copy(path));
38+
}
39+
}
40+
41+
boolean leftAdded = addNode(root.left);
42+
if (leftAdded) {
43+
path.removeLast();
44+
}
45+
46+
boolean rightAdded = addNode(root.right);
47+
if (rightAdded) {
48+
path.removeLast();
49+
}
50+
51+
return true;
52+
}
53+
return false;
54+
}
55+
56+
public List<List<Integer>> pathSum(TreeNode root, int sum) {
57+
if (root == null) {
58+
return result;
59+
}
60+
61+
this.sum = sum;
62+
63+
addNode(root);
64+
65+
return result;
66+
}
67+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.sean.recursive;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.sean.tree.TreeNode;
6+
import org.sean.utils.TreeHelper;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.*;
11+
12+
public class PathSumCounterTest {
13+
14+
@Test
15+
public void pathSum() {
16+
String[] values = {"1", "-2", "-3", "1", "3", "-2", "null", "-1"};
17+
18+
TreeNode root = TreeHelper.buildTreeFrom(values);
19+
20+
PathSumCounter psc = new PathSumCounter();
21+
int pathSum = psc.pathSum(root, 0);
22+
Assert.assertEquals(2, pathSum);
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sean.recursive;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.sean.tree.TreeNode;
6+
import org.sean.utils.TreeHelper;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class RootLeafPathCalculatorTest {
11+
12+
@Test
13+
public void sumNumbers() {
14+
15+
String[] values = {"4", "9", "0", "5", "1"};
16+
TreeNode root = TreeHelper.buildTreeFrom(values);
17+
18+
RootLeafPathCalculator calculator = new RootLeafPathCalculator();
19+
Assert.assertEquals(1026, calculator.sumNumbers(root));
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.sean.recursive;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.sean.tree.TreeNode;
6+
import org.sean.utils.TreeHelper;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class RootLeafPathFinderTest {
11+
12+
@Test
13+
public void hasPathSum() {
14+
String[] values = {"5", "4", "8", "11", "null", "13", "4", "7", "2", "null", "null", "null", "1"};
15+
TreeNode root = TreeHelper.buildTreeFrom(values);
16+
17+
RootLeafPathFinder pathFinder = new RootLeafPathFinder();
18+
Assert.assertTrue(pathFinder.hasPathSum(root, 22));
19+
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.sean.recursive;
2+
3+
import com.google.common.base.Joiner;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.sean.tree.TreeNode;
7+
import org.sean.utils.TreeHelper;
8+
9+
import java.util.Arrays;
10+
import java.util.Collections;
11+
import java.util.LinkedList;
12+
import java.util.List;
13+
14+
import static org.junit.Assert.*;
15+
16+
public class RootLeafSumPathFinderTest {
17+
18+
@Test
19+
public void pathSum() {
20+
// 5,4,8,11,null,13,4,7,2,null,null,5,1
21+
String[] values = {"5", "4", "8", "11", "null", "13", "4", "7", "2", "null", "null", "5", "1",
22+
"null", "null", "null", "null", "null", "null", "null", "null"};
23+
TreeNode root = TreeHelper.buildTreeFrom(values);
24+
25+
List<List<Integer>> lists = new RootLeafSumPathFinder().pathSum(root, 22);
26+
List<String> result = new LinkedList<>();
27+
for (List<Integer> list : lists) {
28+
result.add(Joiner.on(',').join(list));
29+
}
30+
31+
String[] strings = {"5,4,11,2", "5,8,4,5"};
32+
Assert.assertArrayEquals(strings, result.toArray(new String[result.size()]));
33+
}
34+
}

0 commit comments

Comments
 (0)