From 3839cbe1d7af073a18c88f696afa230498abec98 Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 12:34:57 -0700 Subject: [PATCH 1/9] Finished shortest word problem --- src/Practice.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..52c6722 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -12,7 +12,12 @@ 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 oddCount = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 2 != 0) oddCount += nums[i]; + } + return oddCount; } /** From 5077e109e42ada4ff2dd22f91e66bd8c70dedc58 Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 12:46:55 -0700 Subject: [PATCH 2/9] finished shortest word --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 52c6722..d1f9e49 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -32,7 +32,21 @@ 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(); + + int minLength = Integer.MAX_VALUE; + String result = null; + + for (String word : words) { + if (word.length() < minLength || + (word.length() == minLength && word.compareTo(result) < 0)) { + minLength = word.length(); + result = word; + } + } + + return result; } /** From 9271cf3b1b0ad5fcf604c0bc58e798844a56020d Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 14:16:04 -0700 Subject: [PATCH 3/9] Finished adults problem --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index d1f9e49..536f32d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -59,7 +61,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 overEighteen = new HashSet<>(); + for (Map.Entry entry : ages.entrySet()) { + if (entry.getValue() >= 18) { + overEighteen.add(entry.getKey()); + } + } + return overEighteen; } /** From b42b2659f0f50dbf9e86026da8fcca01931bf581 Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 14:24:28 -0700 Subject: [PATCH 4/9] Finished frequencies --- src/Practice.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 536f32d..b2fc14c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -79,7 +79,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(); + int max = Integer.MIN_VALUE; + ListNode curr = head; + while (curr != null) { + if (curr.data > max) max = curr.data; + curr = curr.next; + } + return max; } /** @@ -96,7 +103,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 hash = new HashMap<>(); + ListNode curr = head; + while(curr != null) { + if (hash.containsKey(curr.data)) { + hash.put(curr.data, hash.get(curr.data) + 1); + } else { + hash.put(curr.data, 1); + } + curr = curr.next; + } + return hash; } From b0bf292aae2faf2ef20c2b2f1bc1dee8c39c743e Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 14:29:26 -0700 Subject: [PATCH 5/9] finished level count --- src/Practice.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index b2fc14c..248f28b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -127,7 +127,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 leftLevels = levelCount(root.left); + int rightLevels = levelCount(root.right); + return 1 + Math.max(leftLevels, rightLevels); } From b0ea5a9b2f4a6580f1d3cb9becaa59a0abe78b62 Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 14:33:23 -0700 Subject: [PATCH 6/9] sumAtLevel --- src/Practice.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 248f28b..27ff91d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -158,7 +158,10 @@ 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 a64f0fb7487910d5913c34e07e0004e697721858 Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 14:42:09 -0700 Subject: [PATCH 7/9] finished sum match problem --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 27ff91d..699ffc1 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -176,7 +176,19 @@ 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 treeSum = (root == null) ? 0 : + root.data + sumMatchHelper(root.left) + sumMatchHelper(root.right); + int listSum = 0; + while (head != null) { + listSum += head.data; + head = head.next; + } + return treeSum == listSum; + } + + private static int sumMatchHelper(BinaryTreeNode root) { + if (root == null) return 0; + return root.data + sumMatchHelper(root.left) + sumMatchHelper(root.right); } /** From 98936592384c36bec08a98d06b65fb3d4b71fb7d Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 14:52:18 -0700 Subject: [PATCH 8/9] finished only child --- src/Practice.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 699ffc1..f415aa5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -200,7 +200,12 @@ private static int sumMatchHelper(BinaryTreeNode root) { * @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; } /** @@ -232,9 +237,16 @@ 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; } - /** * Returns the maximum depth of the tree. * From db02393e5d7721f9ee77a849098dd862f8e20f1d Mon Sep 17 00:00:00 2001 From: AI <182692506+AhmedIhsan123@user.noreply.github.com> Date: Wed, 8 Apr 2026 15:02:47 -0700 Subject: [PATCH 9/9] Completed maxdepth --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index f415aa5..ba7ef43 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.List; @@ -282,6 +283,14 @@ 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; + List children = tree.getOrDefault(root, Collections.emptyList()); + if (children.isEmpty()) return 1; + int max = 0; + for (T child : children) { + int childDepth = maxDepth(tree, child); + if (childDepth > max) max = childDepth; + } + return 1 + max; } } \ No newline at end of file