patternjavaMinor
Find all the paths of tree that add to a input value
Viewed 0 times
addtheallpathsvalueinputthatfindtree
Problem
This code is attributed to Stack Overflow itself here. The path which adds to the sum, which need not be starting from the root, not be ending at the leaf, i.e. a path can be between root and a leaf.
I'm looking for code review, optimizations and best practices. I'm also looking for a solution better than \$O(n^2)\$ time complexity, if such exists. I'm also verifying space complexity to be \$O(n)\$, where \$n\$ in both the cases is the number of treenodes.
Also, the code specifically demands me to print, making unit testing not possible. Are there any pointers on how such code could be tested?
```
public class AllPathsAddingSum {
private TreeNode root;
public AllPathsAddingSum(List items) {
create (items);
};
/**
* Constructs a binary tree in order of elements in an array.
* After the number of nodes in the level have maxed, the next
* element in the array would be a child of leftmost node.
*/
private void create(List items) {
if (items.size() == 0) {
throw new IllegalArgumentException("There should atleast be single item in the tree");
}
root = new TreeNode(items.get(0));
final Queue queue = new LinkedList();
queue.add(root);
final int half = items.size() / 2;
for (int i = 0; i ());
traverse(node.left, value);
traverse(node.right, value);
}
private void computePaths(TreeNode node, int sum, int value, List path) {
if (node == null) return;
sum = sum + node.item;
path.add(node.item);
if (sum == value) {
for (Integer i : path) {
System.out.print(i + " ");
}
System.out.println();
}
computePaths(node.left, sum, value, path);
computePaths(node.right, sum, value, path);
path.remove(path.size() - 1);
}
public static void main(String[] args) {
AllPathsAddingSum sumPaths = new AllPathsAd
I'm looking for code review, optimizations and best practices. I'm also looking for a solution better than \$O(n^2)\$ time complexity, if such exists. I'm also verifying space complexity to be \$O(n)\$, where \$n\$ in both the cases is the number of treenodes.
Also, the code specifically demands me to print, making unit testing not possible. Are there any pointers on how such code could be tested?
```
public class AllPathsAddingSum {
private TreeNode root;
public AllPathsAddingSum(List items) {
create (items);
};
/**
* Constructs a binary tree in order of elements in an array.
* After the number of nodes in the level have maxed, the next
* element in the array would be a child of leftmost node.
*/
private void create(List items) {
if (items.size() == 0) {
throw new IllegalArgumentException("There should atleast be single item in the tree");
}
root = new TreeNode(items.get(0));
final Queue queue = new LinkedList();
queue.add(root);
final int half = items.size() / 2;
for (int i = 0; i ());
traverse(node.left, value);
traverse(node.right, value);
}
private void computePaths(TreeNode node, int sum, int value, List path) {
if (node == null) return;
sum = sum + node.item;
path.add(node.item);
if (sum == value) {
for (Integer i : path) {
System.out.print(i + " ");
}
System.out.println();
}
computePaths(node.left, sum, value, path);
computePaths(node.right, sum, value, path);
path.remove(path.size() - 1);
}
public static void main(String[] args) {
AllPathsAddingSum sumPaths = new AllPathsAd
Solution
public AllPathsAddingSum(List items) {
create (items);
};Unneeded space, unneeded semicolon. Consider autoformatting your code.
for (Integer i : path) {
System.out.print(i + " ");
}
System.out.println();This section in
computePaths is doing something different than the function description. It doesn't calculate, it prints.Move it to a separate method. Then give the class a PrintStream to use. This
PrintStream would be System.out, or a dummy PrintStream that just copies everything to an internal buffer.Then you can unittest your code.
Code Snippets
public AllPathsAddingSum(List<Integer> items) {
create (items);
};for (Integer i : path) {
System.out.print(i + " ");
}
System.out.println();Context
StackExchange Code Review Q#74957, answer score: 3
Revisions (0)
No revisions yet.