From 9818a421f26c68b725be4207838b839148ccc665 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 14:23:58 -0700 Subject: [PATCH 01/10] Commpleted oddSum method --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..ec209ee 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -12,7 +12,17 @@ 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 oddSum = 0; + for(int num : nums){ + if(num % 2 != 0){ + oddSum += num; + } + } + return oddSum; } /** From 4886599bbb8f917fd5eef81d7ada20a14f7168a2 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 14:31:48 -0700 Subject: [PATCH 02/10] Completed shortestWord method --- src/Practice.java | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ec209ee..da1d123 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -37,7 +37,32 @@ 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("Set cannot be null"); + } + + if(words.isEmpty()){ + throw new IllegalArgumentException("Set cannot be empty"); + } + + String shortestWord = ""; + for(String word : words){ + if(shortestWord.equals("")){ + shortestWord = word; + } + + if(word.length() < shortestWord.length()){ + shortestWord = word; + }else if (word.length() == shortestWord.length()){ + if(word.compareTo(shortestWord) < 0){ + shortestWord = word; + } + } + + } + + return shortestWord; + } /** From 7c1f8b9698325e4a96f3f654fb8a9dcba7abd04c Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 15:39:02 -0700 Subject: [PATCH 03/10] Completed agesOverEighteen method --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index da1d123..5bf2679 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; @@ -75,7 +76,19 @@ 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("Age cannot be null"); + } + + Set namesOver18 = new HashSet<>(); + for(String name : ages.keySet()){ + if(ages.get(name) >= 18){ + namesOver18.add(name); + } + } + + return namesOver18; + } /** From b97f19e3fa99422139406f143012b82d91fdb17c Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 15:42:59 -0700 Subject: [PATCH 04/10] Completed biggest number method --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 5bf2679..046bb22 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -99,7 +99,19 @@ 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"); + } + + int biggestNumber = Integer.MIN_VALUE; + + ListNode current = head; + + while(current != null){ + biggestNumber = Math.max(biggestNumber, current.data); + current = current.next; + } + return biggestNumber; } /** From a93f237b20bc4e4334b3a0c54110fafe3e1f416c Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 15:56:43 -0700 Subject: [PATCH 05/10] Completed frequency map --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 046bb22..6c69e17 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; @@ -128,7 +129,19 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map frequencyMap = new HashMap<>(); + if(head == null){ + return frequencyMap; + } + + ListNode current = head; + + while(current != null){ + frequencyMap.put(current.data, frequencyMap.getOrDefault(current.data, 0) + 1); + current = current.next; + } + + return frequencyMap; } From 735af186568ac9c86c72d77fb1b9c8a6ba3b0238 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 18:38:26 -0700 Subject: [PATCH 06/10] Completed binary tree levels --- src/Practice.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 6c69e17..58beca5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -154,7 +154,11 @@ 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; + } + + return Math.max(levelCount(root.left), levelCount(root.right)) + 1; } From 37ea5e5f6b33dfd3ffa4a71769994206427dc4a9 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 18:51:59 -0700 Subject: [PATCH 07/10] Complted sumlevel --- src/Practice.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 58beca5..4b2bcad 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -157,7 +157,7 @@ public static int levelCount(BinaryTreeNode root) { if(root == null){ return 0; } - + return Math.max(levelCount(root.left), levelCount(root.right)) + 1; } @@ -186,7 +186,23 @@ 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; + } + + return sumAtLevel(root, level, 1); + } + + public static int sumAtLevel(BinaryTreeNode root, int level, int currentLevel) { + if(root == null){ + return 0; + } + + if(currentLevel == level){ + return root.data; + } + + return sumAtLevel(root.left, level, currentLevel + 1) + sumAtLevel(root.right, level, currentLevel + 1); } From cb71700d9876482644c699b849b3c0bf6c97183c Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 19:06:00 -0700 Subject: [PATCH 08/10] Completed sumMatch method --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 4b2bcad..57faac7 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -217,7 +217,26 @@ public static int sumAtLevel(BinaryTreeNode root, int level, int curren * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; + int listSum = 0; + + ListNode current = head; + + while(current != null){ + listSum += current.data; + current = current.next; + } + + int treeSum = sumTree(root); + + return treeSum == listSum; + } + + private static int sumTree(BinaryTreeNode root){ + if(root == null){ + return 0; + } + + return sumTree(root.left) + sumTree(root.right) + root.data; } /** From 8a2cef8321993494a91a72909b3a6255c8f1a47c Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 19:18:12 -0700 Subject: [PATCH 09/10] Completed nbSum method: --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 57faac7..fe6eeb2 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -247,10 +247,20 @@ private static int sumTree(BinaryTreeNode root){ * @param root the root of the tree * @return the sum of all the tree's values */ - public static int nbSum(TreeNode root) { + public static int nbSum(TreeNode root) { + if (root == null) { return 0; } + int total = root.data; + + for (TreeNode child : root.children) { + total += nbSum(child); + } + + return total; +} + /** * Returns the count of nodes in a non-binary tree that are only children, EXCLUDING the root. * From 75478bf639bb9a58971edf5bc2d05eab047124d8 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Tue, 7 Apr 2026 20:00:01 -0700 Subject: [PATCH 10/10] Completed assignment --- src/Practice.java | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index fe6eeb2..d76a11e 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -259,7 +259,7 @@ public static int nbSum(TreeNode root) { } return total; -} + } /** * Returns the count of nodes in a non-binary tree that are only children, EXCLUDING the root. @@ -290,7 +290,21 @@ 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) { + count += onlyChildCount(child); + } + + if(root.children.size() == 1){ + count++; + } + + return count; } /** @@ -328,6 +342,26 @@ 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 || root == null){ + return 0; + } + + int maxDepth = 1; + + List children = tree.get(root); + + if(children != null){ + for(T child : children){ + maxDepth = Math.max(maxDepth, maxDepth(tree, child)); + } + }else{ + return 0; + } + + + + return 1 + maxDepth; + + } } \ No newline at end of file