Catalog branches: Positive-ink invoice
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. - Add only the numbers in
foundValuesthat are greater than 0. Store the sum inpositiveTotal. - Complete
catalogBranchesPositiveInkInvoiceso it returnspositiveTotal. Return it, do not print it.
Example
If root is [8, 1, 12, -7] and k is 3, then foundValues is [8, 12] and catalogBranchesPositiveInkInvoice returns 20.
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 [6, 0, 16, -12] and k is 7.
TEST DATA 2: root is [4, -6] and k is 7.
Examples
Input: root = [6,0,16,-12] k = 7 Output: 16
foundValues is [16]. Keep [16] and add those numbers. Return 16.
Input: root = [4,-6] k = 7 Output: 0
foundValues is []. Keep [] and add those numbers. Return 0.
Input: root = [] k = 4 Output: 0
foundValues is []. Keep [] and add those numbers. 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.