Catalog branches: Even-key census
Stuck or curious how this works? Visualize BST Search & Insert ↗
Inputs
root is the top node of a binary search tree. Examples list nodes one level at a time, from left to right. null marks a missing child; [] means no tree. Each node stores its number in val. Its left and right links point to children. The runner builds the nodes for you.
Your tasks
- Create a list
foundValues.rootis a binary search tree with no repeated values. In this tree, all values in a left branch are smaller than its parent, and all values in a right branch are larger. Find values fromkthroughk+ 12, including both ends. Put them infoundValuesfrom smallest to largest. Skip branches whose values cannot be in this range. An empty tree gives an emptyfoundValues. - Count the even numbers in
foundValues. Store the count inevenCount. A number is even when it divides by 2 with no remainder. - Complete
catalogBranchesEvenKeyCensusso it returnsevenCount. Return it, do not print it.
Example
If root is [8, -1, 9] and k is 4, then foundValues is [8, 9] and catalogBranchesEvenKeyCensus returns 1.
Rules
- If
foundValuesis empty, return 0. k(a whole number from 1 to 8) is the lowest value to find in task 1; the range ends atk+ 12.
Test data
TEST DATA 1: root is [10, 0, 18, -6] and k is 3.
TEST DATA 2: root is [4, -1] and k is 7.
Examples
Input: root = [10,0,18,-6] k = 3 Output: 1
foundValues is [10]. The even numbers are [10]. There are 1, so return 1.
Input: root = [4,-1] k = 7 Output: 0
foundValues is []. The even numbers are []. There are 0, so return 0.
Input: root = [] k = 4 Output: 0
foundValues is []. The even numbers are []. There are 0, so return 0.
Input limits
- 0 <= node count <= 31; null marks a missing child and does not count as a node.
- k is a whole number from 1 to 8. Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
- Linked lists do not loop back to an earlier node. Tree examples list nodes by level; null means a missing child. The runner builds the nodes shown in the examples.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(h + r); extra memory, including the helper list, O(n). n is the number of nodes; h is the longest root-to-leaf path in the tree; a leaf is a node with no children; r is the number of tree values found. These labels describe how work and memory grow with the input.
Practice focus
Build foundValues first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.