aboutsummaryrefslogtreecommitdiff
path: root/challenge-129/lubos-kolouch/java/ch-1.java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-129/lubos-kolouch/java/ch-1.java')
-rw-r--r--challenge-129/lubos-kolouch/java/ch-1.java30
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-129/lubos-kolouch/java/ch-1.java b/challenge-129/lubos-kolouch/java/ch-1.java
new file mode 100644
index 0000000000..dc905778f9
--- /dev/null
+++ b/challenge-129/lubos-kolouch/java/ch-1.java
@@ -0,0 +1,30 @@
+import java.util.*;
+
+class Main {
+ static Map<Integer, List<Integer>> tree = new HashMap<>();
+ static {
+ tree.put(1, Arrays.asList(2, 3));
+ tree.put(3, Arrays.asList(4));
+ tree.put(4, Arrays.asList(5, 6));
+ }
+
+ public static void main(String[] args) {
+ System.out.println(distance(6, 1, 0));
+ System.out.println(distance(5, 1, 0));
+ System.out.println(distance(2, 1, 0));
+ System.out.println(distance(4, 1, 0));
+ }
+
+ public static Integer distance(int node, int root, int dist) {
+ if (root == node)
+ return dist;
+ if (tree.containsKey(root)) {
+ for (int child : tree.get(root)) {
+ Integer d = distance(node, child, dist + 1);
+ if (d != null)
+ return d;
+ }
+ }
+ return null;
+ }
+}