Branch notebook: Threshold file
Stuck or curious how this works? Visualize Tree Inorder Traversal ↗
Inputs
root is the top node of a binary 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
visitOrder. A binary tree is a set of nodes, each with a value and up to two children: left and right.rootis the top node. Visit a node by recording its value, then visit its whole left branch, then its whole right branch. Use this same rule at every node. Skip missing children.visitOrderis the recorded values. An empty tree gives an emptyvisitOrder. - Count the numbers in
visitOrderthat are at leastk(greater than or equal tok). Store the count inatLeastK. - Complete
branchNotebookThresholdFileso it returnsatLeastK. Return it, do not print it.
Example
If root is [5, null, -6, 5, 5] and k is 7, then visitOrder is [5, -6, 5, 5] and branchNotebookThresholdFile returns 0.
Rules
- If
visitOrderis empty, the count is 0. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: root is [13, null, 8, -11, 13] and k is 5.
TEST DATA 2: root is [5, null, 5, 5, -3] and k is 3.
Examples
Input: root = [13,null,8,-11,13] k = 5 Output: 3
visitOrder is [13, 8, -11, 13]. Here k is 5. The numbers at least 5 are [13, 8, 13]. There are 3, so return 3.
Input: root = [5,null,5,5,-3] k = 3 Output: 3
visitOrder is [5, 5, 5, -3]. Here k is 3. The numbers at least 3 are [5, 5, 5]. There are 3, so return 3.
Input: root = [] k = 2 Output: 0
visitOrder is []. Here k is 2. The numbers at least 2 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(n); extra memory, including the helper list, O(n). n is the number of nodes. These labels describe how work and memory grow with the input.
Practice focus
Build visitOrder 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.