Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Created May 13, 2018 17:46
Show Gist options
  • Save mauricioaniche/b733fcd7742029c65355acaab395108b to your computer and use it in GitHub Desktop.
Save mauricioaniche/b733fcd7742029c65355acaab395108b to your computer and use it in GitHub Desktop.
package perf.junit4.tests.group1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Anderson {
public static class Node {
Node left;
Node right;
int value;
}
public static void allPaths(Node root) {
allPaths(root, Collections.emptyList());
}
public static void allPaths(Node root, List<Integer> nums) {
if(root==null)
return;
List<Integer> copy = add(nums, root.value);
if(root.left == null && root.right == null) {
System.out.println(copy);
} else {
allPaths(root.left, copy);
allPaths(root.right, copy);
}
}
private static List<Integer> add (List<Integer> nums, int value) {
ArrayList<Integer> copy = new ArrayList<>(nums);
copy.add(value);
return copy;
}
public static void main (String[] args) {
Node root = new Node();
root.value = 1;
root.left = new Node();
root.left.value = 2;
root.right = new Node();
root.right.value = 3;
root.left.right = new Node();
root.left.right.value = 5;
allPaths(root);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment