From 0fc77dbff17ad102d40be83a29a5352c1fca0451 Mon Sep 17 00:00:00 2001 From: Akaioka Date: Tue, 7 Apr 2026 14:25:37 -0700 Subject: [PATCH 01/12] Added logic for oddSum --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..e93d9f0 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -12,7 +12,14 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if(nums == null) return 0; + int sum = 0; + for(int num: nums) { + if (num % 2 != 0) { + sum += num; + } + } + return sum; } /** From 7c9ab4caf4bf0e2108de12e158b302f7beaf8149 Mon Sep 17 00:00:00 2001 From: Akaioka Date: Tue, 7 Apr 2026 14:50:08 -0700 Subject: [PATCH 02/12] shortestWord partial --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e93d9f0..6a947eb 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -34,7 +34,22 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + //"kumquat", "date", "fig", "grape", "cherry" + if (words.isEmpty()) { throw new IllegalArgumentException(); } + if (words == null) { throw new NullPointerException(); } + int length = Integer.MAX_VALUE; + String shortest = ""; + for(String word: words){ + if (word.length() < length) { + shortest = word; + length = word.length(); + } else if (word.compareTo(shortest) == -1) { + shortest = word + ; + } + } + + return shortest; } /** From e7d2f7811087e9f136fe68a094f2efae1309f4b4 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Tue, 7 Apr 2026 18:00:53 -0700 Subject: [PATCH 03/12] Added shortestWord method --- src/Practice.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 6a947eb..ec33eeb 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -35,17 +35,19 @@ public static int oddSum(int[] nums) { */ public static String shortestWord(Set words) { //"kumquat", "date", "fig", "grape", "cherry" - if (words.isEmpty()) { throw new IllegalArgumentException(); } if (words == null) { throw new NullPointerException(); } + if (words.isEmpty()) { throw new IllegalArgumentException(); } + int length = Integer.MAX_VALUE; String shortest = ""; for(String word: words){ if (word.length() < length) { shortest = word; length = word.length(); - } else if (word.compareTo(shortest) == -1) { - shortest = word - ; + } else if (word.length() == length) { + if (word.compareTo(shortest) < 0) { + shortest = word; + } } } From 21149f4a266fcf4f7aaebb9f17dd027990eea58f Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Tue, 7 Apr 2026 18:11:10 -0700 Subject: [PATCH 04/12] Added adults method --- src/Practice.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ec33eeb..2f70b5b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -64,7 +65,14 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if (ages == null) { throw new NullPointerException(); } + Set adult = new HashSet<>(); + for(String age: ages.keySet()){ + if (ages.get(age) >= 18) { + adult.add(age); + } + } + return adult; } /** From fce4328eb8347c935a2143d6c8104d7784a1bda2 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Tue, 7 Apr 2026 18:15:33 -0700 Subject: [PATCH 05/12] Added biggestNumber method --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 2f70b5b..8498d61 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -83,7 +83,14 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - return 0; + if (head == null) { throw new IllegalArgumentException();} + ListNode current = head; + int max = head.data; + while (current != null) { + max = Math.max(max, current.data); + current = current.next; + } + return max; } /** From f303372e183db5a8d4a1bed439f88f6ad371c751 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Tue, 7 Apr 2026 18:35:09 -0700 Subject: [PATCH 06/12] Added frequencies method --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 8498d61..31b45fe 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -107,7 +108,18 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + if (head == null) { return new HashMap<>();} + Map frequency = new HashMap<>(); + ListNode current = head; + while (current != null) { + if (!frequency.containsKey(current.data)) { + frequency.put(current.data, 1); + } else { + frequency.put(current.data, frequency.get(current.data) + 1); + } + current = current.next; + } + return frequency; } From 67411ae24719af2295ddecbd480644e4edc000a7 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Tue, 7 Apr 2026 18:45:46 -0700 Subject: [PATCH 07/12] Added levelCount method --- src/Practice.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 31b45fe..5e5a1f4 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -132,7 +132,10 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + if (root == null) { return 0; } + int leftTree = levelCount(root.left) + 1; + int leftRight = levelCount(root.right) + 1; + return Math.max(leftTree, leftRight); } From 4f54d80ad069609d420521a1bc499cd68ba74b23 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 8 Apr 2026 04:19:05 -0700 Subject: [PATCH 08/12] Completed sumMatch method --- src/Practice.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 5e5a1f4..737c15b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -178,7 +178,27 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; + int rootSum = sumMatchHelper(root); + int listSum = sumMatchHelper(head); + return rootSum == listSum ? true : false; + } + + public static int sumMatchHelper(BinaryTreeNode root){ + if (root == null) return 0; + int leftTree = sumMatchHelper(root.left); + int rightTree = sumMatchHelper(root.right); + return root.data + leftTree + rightTree ; + } + + public static int sumMatchHelper(ListNode head){ + if(head == null) return 0; + ListNode current = head; + int sum = 0; + while (current != null) { + sum += current.data; + current = current.next; + } + return sum; } /** From e33fc4af1d9de8d1a2a088765a4d0230d91c0091 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 8 Apr 2026 04:55:48 -0700 Subject: [PATCH 09/12] Completed sumAtLevel Method --- src/Practice.java | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 737c15b..0fa188b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,7 +1,9 @@ import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Queue; import java.util.Set; public class Practice { @@ -163,7 +165,26 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { - return 0; + if (root == null) { return 0;} + + Queue> q = new LinkedList<>(); // 1 + int sum = 0; // 7 + 9 + 2 + int L = 1; // 3 + q.add(root); + + while(!q.isEmpty()){ + for(int i = q.size(); i > 0; i--){ + BinaryTreeNode current = q.poll(); // 7 9 2 + if (L == level) { + sum += current.data; + } + + if (current.left != null){ q.add(current.left);} + if (current.right != null){ q.add(current.right);} + } + L += 1; + } + return sum; } @@ -182,7 +203,7 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h int listSum = sumMatchHelper(head); return rootSum == listSum ? true : false; } - + public static int sumMatchHelper(BinaryTreeNode root){ if (root == null) return 0; int leftTree = sumMatchHelper(root.left); From e60753b43e80f36b8387afd8b0b232b402d2247d Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 8 Apr 2026 13:13:47 -0700 Subject: [PATCH 10/12] Completed nbSum method --- src/Practice.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 0fa188b..596241f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -166,7 +166,7 @@ public static int levelCount(BinaryTreeNode root) { */ public static int sumAtLevel(BinaryTreeNode root, int level) { if (root == null) { return 0;} - + Queue> q = new LinkedList<>(); // 1 int sum = 0; // 7 + 9 + 2 int L = 1; // 3 @@ -231,7 +231,12 @@ public static int sumMatchHelper(ListNode head){ * @return the sum of all the tree's values */ public static int nbSum(TreeNode root) { - return 0; + if (root == null) return 0; + int sum = root.data; + for(TreeNode child: root.children){ + sum += nbSum(child); + } + return sum; } /** From 69e9426bf0fbcf31f18e0e8b9056a90d0c90f285 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 8 Apr 2026 13:24:45 -0700 Subject: [PATCH 11/12] Completed onlyChildCount --- src/Practice.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 596241f..2d38667 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -268,7 +268,15 @@ public static int nbSum(TreeNode root) { * @return the count of nodes that do not have siblings, EXCLUDING THE ROOT */ public static int onlyChildCount(TreeNode root) { - return 0; + if(root == null) return 0; + int count = 0; + + for(TreeNode child: root.children){ + if (root.children.size() == 1) count++; + count += onlyChildCount(child); + } + + return count; } /** From 714d8da1e5188a8e3bd7a399fe1a208c5ab168b2 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 8 Apr 2026 14:44:55 -0700 Subject: [PATCH 12/12] Completed maxDepth Method --- src/Practice.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 2d38667..71634cd 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -313,7 +313,32 @@ public static int onlyChildCount(TreeNode root) { * @param root the root value of the tree * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree */ + /* + /* + Map: Tree: + A -> [B, E, C] A + B -> [E] / | \ + E -> [] B E C + C -> [D, Q] | / \ + D -> [Z] E D Q + Q -> [] \ + Z -> [] Z + */ + public static int maxDepth(Map> tree, T root) { - return 0; + if (root == null || tree == null || !tree.containsKey(root)) return 0; + int depth = maxDepthHelper(tree, root); + return depth; + } + + public static int maxDepthHelper(Map> tree, T root){ + if(!tree.containsKey(root)) return 1; + int depth = 1; + List children = tree.get(root); + + for(T child: children) { + depth = Math.max(maxDepthHelper(tree, child) + 1, depth); + } + return depth; } } \ No newline at end of file