From 56d1424b33229afa1ea76d5aa38f46a654438c55 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Tue, 7 Apr 2026 14:23:50 -0700 Subject: [PATCH 01/11] oddSum --- src/Practice.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..4d36e2c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -12,7 +12,13 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + int sum = 0; + if(nums == null) return sum; + for(int num : nums) { + if(num % 2 != 0) + sum += num; + } + return sum; } /** From 52ae0b8e1d77ea74844e405d97de97c9099ebddf Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Tue, 7 Apr 2026 14:37:56 -0700 Subject: [PATCH 02/11] shortestWord --- src/Practice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 4d36e2c..ec8f620 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -33,7 +33,23 @@ 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.size() == 0) { + throw new IllegalArgumentException(); + } + String shortestWord = null; + for(String word : words) { + if(shortestWord == null) { + shortestWord = word; + continue; + } + if(word.length() < shortestWord.length() || (word.length() == shortestWord.length() && word.compareTo(shortestWord) < 0) ) { + shortestWord = word; + } + } + return shortestWord; } /** From 81e3c002f2fef9bfb9389df4fdf6bde48ff157bf Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Tue, 7 Apr 2026 14:51:50 -0700 Subject: [PATCH 03/11] adults --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ec8f620..e3a9254 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; @@ -62,7 +63,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(); + } + Set eighteenOrOlder = new HashSet<>(); + for(Map.Entry person : ages.entrySet()) { + if(person.getValue() >= 18) { + eighteenOrOlder.add(person.getKey()); + } + } + return eighteenOrOlder; } /** From f2b1b11925cfb8722105d35ecfe58c1db3dd479a Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Wed, 8 Apr 2026 07:37:43 -0700 Subject: [PATCH 04/11] biggest number --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e3a9254..94a65f9 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -83,7 +83,18 @@ 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 biggestNum = -Integer.MIN_VALUE; + while(current != null) { + if(current.data > biggestNum) { + biggestNum = current.data; + } + current = current.next; + } + return biggestNum; } /** From 102f009544975abf6234781e4d14b00b3b03cf72 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Wed, 8 Apr 2026 07:40:13 -0700 Subject: [PATCH 05/11] frequencies --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 94a65f9..d28f9f6 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; @@ -111,7 +112,20 @@ 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) { + T value = current.data; + frequencyMap.put(value, frequencyMap.getOrDefault(value, 0)+1); + current = current.next; + } + + return frequencyMap; } From 2fa5b2a8d49d9779d73d50ee4fa85fd1a184264c Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Wed, 8 Apr 2026 07:42:43 -0700 Subject: [PATCH 06/11] level count --- src/Practice.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index d28f9f6..f3b2389 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -138,7 +138,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 1 + Math.max(levelCount(root.left), levelCount(root.right)); } From 13eb858610070c7fb51ff3a8da774526d8ac3e28 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Wed, 8 Apr 2026 07:46:11 -0700 Subject: [PATCH 07/11] sumAtLevel --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index f3b2389..63c0ca9 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -170,7 +170,14 @@ 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 < 1) { + return 0; + } + if(level == 1) { + return root.data; + } + + return sumAtLevel(root.left, level-1) + sumAtLevel(root.right, level-1); } From 4f1332b0af6d4861f71fbf4aa99fafdc9ee7295e Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Wed, 8 Apr 2026 07:50:36 -0700 Subject: [PATCH 08/11] summatch --- src/Practice.java | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 63c0ca9..0b86f12 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.Stack; public class Practice { /** @@ -192,7 +193,33 @@ 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; + Stack> stk = new Stack<>(); + if(root != null) { + stk.push(root); + } + + int treeSum = 0; + int listSum = 0; + + while(!stk.isEmpty()) { + BinaryTreeNode node = stk.pop(); + treeSum += node.data; + + if(node.left != null) { + stk.push(node.left); + } + if(node.right != null) { + stk.push(node.right); + } + } + + ListNode current = head; + while(current != null) { + listSum += current.data; + current = current.next; + } + + return treeSum == listSum; } /** From 139cbdb8d99091b1088c5bfd417fbbccde19fc26 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Wed, 8 Apr 2026 07:54:56 -0700 Subject: [PATCH 09/11] nbSum --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 0b86f12..223a496 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -231,7 +231,17 @@ 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) { + sum += nbSum(child); + } + + return sum; } /** From dbe1f368f9ab08c8ddb506297440eb56ba3b2fdf Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Wed, 8 Apr 2026 07:56:38 -0700 Subject: [PATCH 10/11] onlyChildCount --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 223a496..7bb6866 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -273,7 +273,20 @@ 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; + if(root.children.size() == 1) { + count++; + } + + for(TreeNode child : root.children) { + count += onlyChildCount(child); + } + + return count; } /** From 995c61a9f18292f4cebd7e66581f91ca9370dbeb Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Wed, 8 Apr 2026 08:07:10 -0700 Subject: [PATCH 11/11] maxdepth --- src/Practice.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 7bb6866..0a8e3cf 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -324,6 +324,28 @@ 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; + + //find root and if its actually in the tree + boolean rootInTree = tree.containsKey(root); + if (!rootInTree) { + for (List children : tree.values()) { + if (children.contains(root)) { + rootInTree = true; + break; + } + } + } + if(!rootInTree) return 0; + //check if its a leaf + if(!tree.containsKey(root)) return 1; + + int maxDepth = 0; + for(T child : tree.get(root)) { + int depth = maxDepth(tree, child); + maxDepth = Math.max(depth, maxDepth); + } + + return 1+maxDepth; } } \ No newline at end of file