From 6a87c4280465b01ba3d8ef4a21db711d3aa78c1e Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 7 Apr 2026 19:10:46 -0700 Subject: [PATCH 01/11] oddSum complete --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..cea09a9 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 n : nums) { + if(n % 2 != 0) { + sum += n; + } + } + return sum; } /** From 667d8528c569b89cda4c0065c2db7baac7ae5781 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 7 Apr 2026 19:14:51 -0700 Subject: [PATCH 02/11] adults method passed --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index cea09a9..2dcc45b 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; @@ -47,7 +48,16 @@ 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("Ages map cannot be null."); + Set namesAdult = new HashSet<>(); + for(Map.Entry entry : ages.entrySet()) { + String key = entry.getKey(); + int value = entry.getValue(); + if(value >= 18) { + namesAdult.add(key); + } + } + return namesAdult; } /** From 288b2f8079254667789416c8627da2c58731d8a5 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 7 Apr 2026 19:17:42 -0700 Subject: [PATCH 03/11] biggestNumber complete --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 2dcc45b..d3db6e3 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -68,7 +68,16 @@ 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("Head cannot be null."); + ListNode current = head; + int biggest = head.data; + while(current != null) { + if(current.data > biggest) { + biggest = current.data; + } + current = current.next; + } + return biggest; } /** From 5767780c90f1d90eecc67794409ad6d3dbf9b678 Mon Sep 17 00:00:00 2001 From: allenres Date: Wed, 8 Apr 2026 17:42:58 -0700 Subject: [PATCH 04/11] levelCount complete --- src/Practice.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index d3db6e3..ffc000f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -107,7 +107,12 @@ 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; + if(root.left == null && root.right == null) return 1; + int leftLevels = levelCount(root.left) + 1; + int rightLevels = levelCount(root.right) + 1; + if (leftLevels > rightLevels) return leftLevels; + return rightLevels; } From e811dbe41314f00d448d0f0a506e850e1c7819c3 Mon Sep 17 00:00:00 2001 From: allenres Date: Wed, 8 Apr 2026 18:05:21 -0700 Subject: [PATCH 05/11] nb sum complete --- src/Practice.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ffc000f..a6b0d0f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -167,7 +167,13 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @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) { + int subTreeSum = nbSum(child); + sum += subTreeSum; + } + return sum; } /** From bb0ca9fb4573ab4e5fde5a6b0a16f94d0c1e631b Mon Sep 17 00:00:00 2001 From: allenres Date: Wed, 8 Apr 2026 18:14:57 -0700 Subject: [PATCH 06/11] sum match complete --- src/Practice.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index a6b0d0f..3b8ce3a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +1,8 @@ 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 { @@ -155,6 +157,25 @@ 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) { + if(head == null && root == null) return true; + if(head == null || root == null) return false; + int listSum = 0; + int treeSum = 0; + ListNode current = head; + while(current != null) { + listSum += current.data; + current = current.next; + } + + Queue> queue = new LinkedList<>(); + queue.add(root); + while(!queue.isEmpty()) { + BinaryTreeNode currentNode = queue.remove(); + treeSum += currentNode.data; + if(currentNode.left != null) queue.add(currentNode.left); + if(currentNode.right != null) queue.add(currentNode.right); + } + if (treeSum == listSum) return true; return false; } From c6f72a97b7b6fb75710aef1c893ea9f3c595e228 Mon Sep 17 00:00:00 2001 From: allenres Date: Wed, 8 Apr 2026 18:24:37 -0700 Subject: [PATCH 07/11] frequencies method complete --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 3b8ce3a..38a27a5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,9 +1,11 @@ +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; +import java.util.TreeMap; public class Practice { /** @@ -96,7 +98,14 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map freqMap = new HashMap<>(); + if (head == null) return freqMap; + ListNode current = head; + while(current != null) { + freqMap.put(current.data, freqMap.getOrDefault(current.data, 0) + 1); + current = current.next; + } + return freqMap; } From c414a00a8ba7a3afdc47e1db625a0c485395c073 Mon Sep 17 00:00:00 2001 From: allenres Date: Wed, 8 Apr 2026 18:46:38 -0700 Subject: [PATCH 08/11] sumAtLevel complete --- src/Practice.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 38a27a5..03ae61c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -39,7 +40,15 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if(words == null) throw new NullPointerException(); + if(words.isEmpty()) throw new IllegalArgumentException(); + + List sorted = new LinkedList<>(); + for(String word : words) { + sorted.add(word); + } + Collections.sort(sorted); + return sorted.get(0); } /** @@ -151,7 +160,11 @@ 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 || level <= 0) return 0; + + if(level == 1) return root.data; + + return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1); } From 96ad76001171a224cc53b519547bc3777c51d65e Mon Sep 17 00:00:00 2001 From: allenres Date: Wed, 8 Apr 2026 18:49:17 -0700 Subject: [PATCH 09/11] shortestWord complete --- src/Practice.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 03ae61c..c89028a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -43,12 +43,20 @@ public static String shortestWord(Set words) { if(words == null) throw new NullPointerException(); if(words.isEmpty()) throw new IllegalArgumentException(); - List sorted = new LinkedList<>(); - for(String word : words) { - sorted.add(word); + String shortest = null; + + for (String word : words) { + if(shortest == null) { + shortest = word; + } else if (word.length() < shortest.length()) { + shortest = word; + } else if (word.length() == shortest.length()) { + if (word.compareTo(shortest) < 0) { + shortest = word; + } + } } - Collections.sort(sorted); - return sorted.get(0); + return shortest; } /** From f522b009402fce1d45d1567afe82a4b28115ed78 Mon Sep 17 00:00:00 2001 From: allenres Date: Wed, 8 Apr 2026 18:53:57 -0700 Subject: [PATCH 10/11] onlyChildCount complete --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index c89028a..95e7042 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -256,7 +256,18 @@ 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 childNode : root.children) { + if(root.children.size() == 1) { + count = 1; + } + count += onlyChildCount(childNode); + } + + return count; } /** From b1ecbc269518cc90ac7baeb10a9fb6f63b7cf9a5 Mon Sep 17 00:00:00 2001 From: allenres Date: Wed, 8 Apr 2026 19:16:22 -0700 Subject: [PATCH 11/11] maxDepth complete --- src/Practice.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 95e7042..2e4af77 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -305,6 +305,27 @@ public static int onlyChildCount(TreeNode root) { * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree */ public static int maxDepth(Map> tree, T root) { - return 0; + if (tree == null || !tree.containsKey(root)) { + return 0; + } + + List children = tree.get(root); + if (children.isEmpty()) { + return 1; + } + int maxDepth = 0; + for (T child : children) { + int childDepth; + + if (!tree.containsKey(child)) { + childDepth = 1; + } else { + childDepth = maxDepth(tree, child); + } + + maxDepth = Math.max(maxDepth, childDepth); + } + + return 1 + maxDepth; } } \ No newline at end of file