From 5508d47166e6da23c7c7855776c955aa6dae437c Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:36:29 -0700 Subject: [PATCH 01/12] update imports to match with methods --- src/Practice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..f7c923a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +1,10 @@ import java.util.List; import java.util.Map; +import java.util.HashMap; +import java.util.HashSet; import java.util.Set; +import java.util.Queue; +import java.util.LinkedList; public class Practice { /** From 6de9e85243c1799defd472ef42e28ac74fd52731 Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:37:59 -0700 Subject: [PATCH 02/12] Implement oddSum --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index f7c923a..688cdf2 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -16,9 +16,19 @@ 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 sum = 0; + + for (int n : nums) { + if (n % 2 != 0) { + sum += n; + } + } + return sum; } + /** * Returns the shortest word in the Set. * From 0d2aaa1be50a94296561e2927973b42d855a1047 Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:40:21 -0700 Subject: [PATCH 03/12] Implement shortestWord --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 688cdf2..90b71a5 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -41,9 +41,21 @@ 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 shortest = null; + + for (String word : words) { + if (shortest == null + || word.length() < shortest.length() + || (word.length() == shortest.length() && word.compareTo(shortest) < 0)) { + shortest = word; + } + } + return shortest; } + /** * Returns a set of all the names of people that are 18 years of age or older. * From 8f7e12e3bbd66ff0d977a1798118b58e9e81a478 Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:41:07 -0700 Subject: [PATCH 04/12] Implement adults --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 90b71a5..e90b0fa 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -66,9 +66,16 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + Set result = new HashSet<>(); + for (Map.Entry entry : ages.entrySet()) { + if (entry.getValue() >= 18) { + result.add(entry.getKey()); + } + } + return result; } + /** * Returns the biggest number in a linked list. * From bff3c8ab94924d8b84bff71c4b30461420e05c7c Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:42:42 -0700 Subject: [PATCH 05/12] Implement biggestNumber --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e90b0fa..651dd27 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -84,9 +84,22 @@ 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 = head.data; + + ListNode current = head.next; + + while (current != null) { + if (current.data > max) { + max = current.data; + } + current = current.next; + } + return max; } + /** * Returns a frequency map counting how frequently items appear in a linked list. * From 549e4aa83016b4735ac5da81cde78de170fdaed0 Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:43:37 -0700 Subject: [PATCH 06/12] Implement frequencies --- src/Practice.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 651dd27..a8a5fc6 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -113,11 +113,19 @@ public static int biggestNumber(ListNode head) { * @param head the head of the list * @return a frequency map of values in the list */ - public static Map frequencies(ListNode head) { - return null; +public static Map frequencies(ListNode head) { + Map map = new HashMap<>(); + + ListNode current = head; + while (current != null) { + map.put(current.data, map.getOrDefault(current.data, 0) + 1); + current = current.next; + } + return map; } + /** * Returns the number of levels in the tree. * From 99e51987b676d51ad65c01a922d5d410f60b22f1 Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:47:18 -0700 Subject: [PATCH 07/12] Implement levelCount --- src/Practice.java | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index a8a5fc6..0de4b00 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -113,7 +113,7 @@ public static int biggestNumber(ListNode head) { * @param head the head of the list * @return a frequency map of values in the list */ -public static Map frequencies(ListNode head) { + public static Map frequencies(ListNode head) { Map map = new HashMap<>(); ListNode current = head; @@ -135,8 +135,33 @@ 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 k = 0; + Queue> queue = new LinkedList<>(); + queue.add(root); + + + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + + + BinaryTreeNode node = queue.poll(); + if (node.left != null) { + queue.add(node.left); + } + if (node.right != null) { + queue.add(node.right); + } + } + k++; } + return k; +} + /** From aaf44db3437e997ce447a37e326628d5418c9d3f Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:48:02 -0700 Subject: [PATCH 08/12] Implement sumAtLevel --- src/Practice.java | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 0de4b00..272f5a4 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -188,8 +188,37 @@ 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; + } + + int k = 1; + int sum = 0; + Queue> queue = new LinkedList<>(); + queue.add(root); + + + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + + + BinaryTreeNode node = queue.poll(); + if (node.left != null) { + queue.add(node.left); + } + if (node.right != null) { + queue.add(node.right); + } + if (k == level) { + sum += node.data; + } + } + k++; } + return sum; +} + /** From 6f575c9402cbd2e5a093022c1e9c3ae3ba6e4ddf Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:51:41 -0700 Subject: [PATCH 09/12] Implement sumMatch --- src/Practice.java | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 272f5a4..088c3bc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -232,9 +232,43 @@ 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 = 0; + int linkedListSum = 0; + + + if (root != null) { + Queue> queue = new LinkedList<>(); + queue.add(root); + + + while (!queue.isEmpty()) { + BinaryTreeNode node = queue.poll(); + + + if (node.left != null) { + queue.add(node.left); + } + if (node.right != null) { + queue.add(node.right); + } + + + treeSum += node.data; + } } + + ListNode current = head; + while (current != null) { + linkedListSum += current.data; + current = current.next; + } + + + return treeSum == linkedListSum; +} + + /** * Returns the sum of all the nodes in a non-binary tree. * From e6ac36ee8260f03039cef8277f014cd9052eaa7e Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:54:06 -0700 Subject: [PATCH 10/12] Implement nbSum --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 088c3bc..20e004d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -278,9 +278,19 @@ 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; } + /** * Returns the count of nodes in a non-binary tree that are only children, EXCLUDING the root. * From 16ef1c1339596f3e535580f2bac1c253ed78de58 Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:55:25 -0700 Subject: [PATCH 11/12] Implement onlyChildCount --- src/Practice.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 20e004d..2fc9324 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -320,8 +320,30 @@ 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; + Queue> queue = new LinkedList<>(); + queue.add(root); + + + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + + + if (node.children.size() == 1) { + count++; + } + + + for (TreeNode child : node.children) { + queue.add(child); + } } + return count; + } + /** * Returns the maximum depth of the tree. From dbdf88979ea41b8e0005acc1c0cb2d3396b66f2d Mon Sep 17 00:00:00 2001 From: konradkelly Date: Wed, 8 Apr 2026 23:56:26 -0700 Subject: [PATCH 12/12] Implement maxDepth --- src/Practice.java | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 2fc9324..609c33d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -380,6 +380,34 @@ 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; + + + int k = 0; + Queue queue = new LinkedList<>(); + queue.add(root); + + + while (!queue.isEmpty()) { + int sizeAtK = queue.size(); + for (int i = 0; i < sizeAtK; i++) { + T node = queue.poll(); + List children = tree.get(node); + + + if (children != null) { + for (T child : children) { + queue.add(child); + } + } + } + k++; } -} \ No newline at end of file + + +return k; + } +}