From 2a092f4ea28d82c2f5063034295da44d1094a7db Mon Sep 17 00:00:00 2001 From: Omnissiah <234093135+Zeta1313@users.noreply.github.com> Date: Wed, 8 Apr 2026 23:44:00 -0700 Subject: [PATCH 1/2] late-night coding --- src/Practice.java | 88 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 9 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..b2d6b79 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; @@ -12,7 +14,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 output = 0; + for (int i: nums) { + if (i%2!=0) { + output = output + i; + } + } + return output; } /** @@ -27,7 +36,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(); + String output = "pneumonoultramicroscopicsilicovolcanoconiosis"; + for (String x : words) { + if (x.length() < output.length() || (x.length() == output.length() && x.compareTo(output) < 0)) { + output = x; + } + } + return output; } /** @@ -40,7 +57,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(); + HashSet output = new HashSet<>(); + for (String i : ages.keySet()) { + if (ages.get(i) >= 18) { + output.add(i); + } + } + return output; } /** @@ -51,7 +75,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(); + + ListNode current = head; + int output = current.data; + + while (current != null) { + if (current.data > output) { + output = current.data; + } + current = current.next; + } + + return output; } /** @@ -68,7 +104,18 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + HashMap output = new HashMap<>(); + ListNode current = head; + while (current!=null) { + if (output.containsKey(current.data)) { + output.put(current.data, output.get(current.data) + 1); + } + else { + output.put(current.data, 1); + } + current = current.next; + } + return output; } @@ -81,7 +128,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 x = levelCount(root.left); + int y = levelCount(root.right); + if (x > y) return x+1; + else return y+1; } @@ -109,7 +161,9 @@ 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; + if (level == 1) return root.data; + return sumAtLevel(root.left, level-1) + sumAtLevel(root.right, level-1); } @@ -124,7 +178,18 @@ 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 output = 0; + ListNode current = head; + while (current != null) { + output += current.data; + current = current.next; + } + return output == sumMatchHelper(root); + } + + public static int sumMatchHelper(BinaryTreeNode root) { + if (root == null) return 0; + return root.data + sumMatchHelper(root.left) + sumMatchHelper(root.right); } /** @@ -136,7 +201,12 @@ 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 output = 0; + for (TreeNode x : root.children) { + output = output + nbSum(x); + } + return output+root.data; } /** From 64825f580380a87bb7472b2078fece66f990bf6d Mon Sep 17 00:00:00 2001 From: Omnissiah <234093135+Zeta1313@users.noreply.github.com> Date: Thu, 9 Apr 2026 00:07:57 -0700 Subject: [PATCH 2/2] Just realized that I didn't actually make the actual commit, my bad --- src/Practice.java | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index b2d6b79..f578fcc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -237,9 +237,17 @@ public static int nbSum(TreeNode root) { * @param root the root of the tree * @return the count of nodes that do not have siblings, EXCLUDING THE ROOT */ - public static int onlyChildCount(TreeNode root) { - return 0; +public static int onlyChildCount(TreeNode root) { + if (root == null) return 0; + int count = 0; + for (TreeNode child : root.children) { + if (root.children.size() == 1) { + count += 1; + } + count += onlyChildCount(child); } + return count; +} /** * Returns the maximum depth of the tree. @@ -276,6 +284,20 @@ 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) return 0; + if (!tree.containsKey(root)) return 0; + if (root == null) return 0; + return maxDepthHelper(tree, root); + } + + public static int maxDepthHelper(Map> tree, T root) { + List children = tree.get(root); + if (children == null || children.isEmpty()) return 1; + int output = 0; + for (T x : children) { + int y = maxDepthHelper(tree, x); + if (output < y) output = y; + } + return output + 1; } } \ No newline at end of file