forked from lemonbashar/java-algo-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchSums.java
More file actions
56 lines (47 loc) · 1.25 KB
/
BranchSums.java
File metadata and controls
56 lines (47 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package algoexpert.easy;
import java.util.ArrayList;
import java.util.List;
/*
PROBLEM:
Given a binary tree, find sum of all branches. Returned in left to right order.
EXAMPLE:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ \
8 9 10 -> [15, 16, 18, 10, 11]
SOLUTION:
1. Recursion (maintain currentSum) -> time : O(n) | space : O(n)
*/
public class BranchSums
{
public static class BinaryTree {
int value;
BinaryTree left;
BinaryTree right;
BinaryTree(int value) {
this.value = value;
this.left = null;
this.right = null;
}
}
public static void addSums(BinaryTree current, int currentSum, ArrayList<Integer> solution)
{
if (current == null) return;
currentSum += current.value;
if (current.left == null && current.right == null)
{
solution.add(currentSum);
return;
}
addSums(current.left, currentSum, solution);
addSums(current.right, currentSum, solution);
}
public static List<Integer> branchSums(BinaryTree root) {
ArrayList<Integer> solution = new ArrayList<Integer> ();
addSums(root, 0, solution);
return solution;
}
}