From 2f988a9fb91fa82d7fcd12a9201da008539b8364 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Tue, 7 Apr 2026 23:04:02 -0700 Subject: [PATCH 01/11] implemented oddSum --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..ca62a38 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 || nums.length == 0){ + return 0; + } + + int sum = 0; + for(int i = 0; i < nums.length; i++) { + if(nums[i] % 2 != 0){ + sum += nums[i]; + } + } + return sum; } /** From 03fa8d70a8c15f3af7c9855b6b58e5c5c1e4ae8b Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Tue, 7 Apr 2026 23:58:13 -0700 Subject: [PATCH 02/11] implemented shortestWord --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ca62a38..fe83fe9 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -37,7 +37,26 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if(words.isEmpty()) { + throw new IllegalArgumentException(); + } + + String shortestWord = ""; + + for(String word : words) { + if(shortestWord == "") { + shortestWord = word; + } + if(word.length() < shortestWord.length()) { + shortestWord = word; + } + if(word.length() == shortestWord.length()){ + if(word.compareTo(shortestWord) < 0){ + shortestWord = word; + } + } + } + return shortestWord; } /** From 38a1191a8b43df6ec3bf7563c8c794cbd6c854b4 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 13:43:56 -0700 Subject: [PATCH 03/11] implemented adults --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index fe83fe9..64cc1f9 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; @@ -69,7 +70,20 @@ 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 names = new HashSet<>(); + + for(Map.Entry entry: ages.entrySet()){ + String name = entry.getKey(); + int age = entry.getValue(); + if (age >= 18) { + names.add(name); + } + } + return names; } /** From 67e75a834a8cccac46f809651f5bf3185bcfc840 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 14:01:09 -0700 Subject: [PATCH 04/11] implemented biggestNumber --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 64cc1f9..ff15711 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -94,7 +94,20 @@ 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 = current.data; + while (current != null){ + if (current.data > biggestNum) { + biggestNum = current.data; + } + current = current.next; + } + return biggestNum; } /** From 450fa852a24ce37150d92fff7e674e5b2d7d7ca9 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 14:19:07 -0700 Subject: [PATCH 05/11] implemented frequencies --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ff15711..5dc3c75 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; @@ -124,7 +125,25 @@ 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 map = new HashMap(); + + ListNode current = head; + + while (current != null){ + if (map.containsKey(current.data)){ + int currentCount = map.get(current.data); + map.put(current.data, currentCount + 1); + } + else { + map.put(current.data, 1); + } + current = current.next; + } + return map; } From deea982cbb00ee06b54a3cb722f76b9f99d4ea5e Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 14:42:57 -0700 Subject: [PATCH 06/11] implemented levelCount --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 5dc3c75..ed01e4c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -156,7 +156,14 @@ 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 left = levelCount(root.left); + int right = levelCount(root.right); + + return Math.max(left, right) + 1; } From 982d7f497d7700be46ad97daf4aff354969e0099 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 19:53:06 -0700 Subject: [PATCH 07/11] implemented sumAtLevel --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ed01e4c..9e0be07 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -191,7 +191,18 @@ 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; + } + + int left = sumAtLevel(root.left, level - 1); + int right = sumAtLevel(root.right, level - 1); + + return left + right; } From 19f725340e4641332524619905f2feb7eefa9169 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 20:09:11 -0700 Subject: [PATCH 08/11] implemented sumMatch --- src/Practice.java | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 9e0be07..fa92cd9 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 { /** @@ -217,7 +218,40 @@ 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> stack = new Stack<>(); + + if (root != null) { + stack.push(root); + } + + int treeSum = 0; + int listSum = 0; + + while (!stack.isEmpty()) { + BinaryTreeNode current = stack.pop(); + + treeSum = treeSum + current.data; + + if (current.left != null) { + stack.push(current.left); + } + + if (current.right != null) { + stack.push(current.right); + } + } + + while (head != null) { + listSum = listSum + head.data; + head = head.next; + } + + if (treeSum == listSum) { + return true; + } + else { + return false; + } } /** From 32fe0cea3c6352976b6f0dc5b3e45e16fec1e0a3 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 20:24:16 -0700 Subject: [PATCH 09/11] implemented nbSum --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index fa92cd9..d22d845 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -263,7 +263,14 @@ 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 (int i = 0; i < root.children.size(); i++) { + sum = sum + nbSum(root.children.get(i)); + } + return sum; } /** From b933919e75b092f72887d785249d1ea972e91487 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 20:51:04 -0700 Subject: [PATCH 10/11] implemented onlyChildCount --- src/Practice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index d22d845..189f0d7 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -302,7 +302,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 + 1; + } + count = count + onlyChildCount(child); + } + return count; } /** From e8b2110c133cc12dd81b5b45423af3f14e129287 Mon Sep 17 00:00:00 2001 From: Lilian <210286008+liliann19@users.noreply.github.com> Date: Wed, 8 Apr 2026 22:24:24 -0700 Subject: [PATCH 11/11] implemented maxDepth --- src/Practice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 189f0d7..8878a5f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -349,6 +349,22 @@ 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.get(root); + + if (children == null) return 0; + + 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 maxDepth + 1; } } \ No newline at end of file