Count Level-Order Traversals of a Tree
Updated
Problem statement
A rooted tree has n nodes numbered from 0 to n - 1, and node 0 is the root. You are given an array parent of length n: parent[0] = -1, and for every other node i, parent[i] is the parent of node i. A node may have any number of children.
A level-order traversal visits the nodes with a queue. Start with only the root in the queue. Then, while the queue is not empty, take the node at the front of the queue, visit it, and add all of its children to the back of the queue. The children of one node may be added in any order, so different choices can give different visiting orders.
Return the number of different orders in which a level-order traversal can visit the nodes. The number can be very large, so return it modulo 10^9 + 7.
Function
countLevelOrders(parent: int[]) → intExamples
Example 1
parent = [-1,0,0,1,1,2,2,2]return = 24Node 0 has children 1 and 2, node 1 has children 3 and 4, and node 2 has children 5, 6 and 7. These groups can be added in 2, 2 and 6 orders. Every combination of choices gives a different visiting order, so there are 2 * 2 * 6 = 24. One of them is 0, 2, 1, 7, 5, 6, 4, 3: node 2 is visited before node 1, so its children also come before the children of node 1.
Example 2
parent = [-1]return = 1The tree is only the root, so the one visiting order is 0.
Example 3
parent = [-1,0,0,0,0,0,0,0,0,0,0,0,0,0]return = 227020758The root has 13 children, which can be added in 13! = 6,227,020,800 orders. Modulo 10^9 + 7 this is 6,227,020,800 - 6 * 1,000,000,007 = 227,020,758.
Constraints
1 <= parent.length <= 10^4parent[0] = -10 <= parent[i] < parent.lengthfor everyi >= 1- Following parents from any node always reaches node
0, soparentdescribes one tree rooted at node0.
More Bank of America Online Assessment questions
- Return Exact Cash Register ChangeOA · Seen Aug 2026
- Sum Skipping Multiples of 7OA · Seen Nov 2025
- Wildcard Substring MatchOA · Seen Nov 2025
- Maximum String BeautyOA · Seen Sep 2024
- Merge Point of Two Linked ListsOA · Seen Oct 2023
- Remove Duplicates from a ListOA · Seen Oct 2023
- Pairs with Target Sum in a Sorted ArrayOA · Seen Oct 2023
- Count Set BitsOA · Seen Sep 2023
- Count Pairs with Sum KOA · Seen Jun 2023
- Letter Pattern SumOA · Seen Sep 2022
Similar questions at other companies
- Count Distinct Binary Search TreesC3.ai · OAC3.ai interview questions
- Stable Workload Redistribution TreesTower Research Capital · OA
- Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Count Prime Values in SubtreesVisa · OAVisa interview questions
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Minimum N-ary Tree Depth DeletionsSnowflake · OA · PHONE SCREENSnowflake interview questions
- Tree Pythagorean TriplesIBM · OAIBM interview questions
- Count LevelsBlackRock · OABlackRock interview questions
- Count Reachable Values by Halving and DecrementingInfosys · OAInfosys interview questions