aboutsummaryrefslogtreecommitdiff
path: root/challenge-129/lubos-kolouch/java/ch-1.java
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-10-02 20:40:22 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-10-02 20:40:22 +0800
commit423235bdacf1c548dd8e09c163b7f78668c1df3e (patch)
tree23dc6c139df775332aa5ed916831da8572380e93 /challenge-129/lubos-kolouch/java/ch-1.java
parent30395f82ec7a8df0b302ef4ea04384d26633c6a8 (diff)
parent50ee9dd9832e336236b31f8b33237b4df38650c0 (diff)
downloadperlweeklychallenge-club-423235bdacf1c548dd8e09c163b7f78668c1df3e.tar.gz
perlweeklychallenge-club-423235bdacf1c548dd8e09c163b7f78668c1df3e.tar.bz2
perlweeklychallenge-club-423235bdacf1c548dd8e09c163b7f78668c1df3e.zip
Merge branch 'master' of github.com:seaker/perlweeklychallenge-club
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;
+ }
+}