You are given an integer n. There is a complete binary tree with 2n - 1 nodes. The root of that tree is the node with the value 1, and every node with a value val in the range [1, 2n - 1 - 1] has two children where:
2 * val, and2 * val + 1.You are also given a 2D integer array queries of length m, where queries[i] = [ai, bi]. For each query, solve the following problem:
ai and bi.ai and bi.Note that:
Return an array answer of length m where answer[i] is the answer to the ith query.
Example 1:
Input: n = 3, queries = [[5,3],[4,7],[2,3]] Output: [4,5,3] Explanation: The diagrams above show the tree of 23 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge. - After adding the edge between nodes 3 and 5, the graph contains a cycle of nodes [5,2,1,3]. Thus answer to the first query is 4. We delete the added edge and process the next query. - After adding the edge between nodes 4 and 7, the graph contains a cycle of nodes [4,2,1,3,7]. Thus answer to the second query is 5. We delete the added edge and process the next query. - After adding the edge between nodes 2 and 3, the graph contains a cycle of nodes [2,1,3]. Thus answer to the third query is 3. We delete the added edge.
Example 2:
Input: n = 2, queries = [[1,2]] Output: [2] Explanation: The diagram above shows the tree of 22 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge. - After adding the edge between nodes 1 and 2, the graph contains a cycle of nodes [2,1]. Thus answer for the first query is 2. We delete the added edge.
Constraints:
2 <= n <= 30m == queries.length1 <= m <= 105queries[i].length == 21 <= ai, bi <= 2n - 1ai != biWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks for the length of the cycle formed when an edge is added between two nodes in a complete binary tree. The length of the cycle is equal to the distance between the two nodes in the tree plus one for the added edge. By iteratively dividing the larger node value by two, we find their lowest common ancestor and count the tree edges traversed.
Here's how the algorithm would work step-by-step:
def cycle_length_queries(tree_depth: int, queries: list[list[int]]) -> list[int]:
# Store the results for each query
results = []
for start_node, end_node in queries:
# Track the number of edges along the path connecting both nodes
path_length = 1
# Move up the tree until both nodes meet at their lowest common ancestor
while start_node != end_node:
if start_node > end_node:
start_node //= 2
else:
end_node //= 2
path_length += 1
# Append the total cycle length including the newly added edge
results.append(path_length)
return results
In a complete binary tree where node values correspond to standard 1-based heap indexing, the parent of any node can be found by integer division by two. Adding an edge between two nodes creates a cycle consisting of the added edge plus the unique tree path between them. We can determine the length of this path by iteratively moving the deeper node up to its parent until both nodes converge at their lowest common ancestor.
Here's how the algorithm would work step-by-step:
def cycle_length_queries(tree_depth: int, queries: list[list[int]]) -> list[int]:
# Store the cycle length result for each query
results = []
for start_node, end_node in queries:
cycle_edges = 1
# Move the deeper node up until both nodes meet at their lowest common ancestor
while start_node != end_node:
if start_node > end_node:
start_node //= 2
else:
end_node //= 2
cycle_edges += 1
# Append the calculated cycle length for the current query
results.append(cycle_edges)
return results
| Case | How to Handle |
|---|---|
| Queries involving nodes that are identical, meaning the start and end of the query are the same node | The algorithm must return a cycle length of 1 since adding an edge from a node to itself creates a self-loop of length 1. |
| Minimum tree size where n equals 1, resulting in a single root node with value 1 | Since only node 1 exists, any query will involve the root and must return a cycle length of 1. |
| Maximum possible value of n up to 30, creating node values up to 2 raised to the power of 30 minus 1 | Node values can exceed standard 32-bit signed integer limits, requiring 64-bit integers to prevent arithmetic overflow during binary tree traversal. |
| Path between nodes that goes through the root of the complete binary tree | The Lowest Common Ancestor logic must correctly ascend both nodes independently until they intersect, properly accounting for the path length through the root. |
| Queries where one node is an ancestor of the other node in the tree hierarchy | The depth-tracking loop will terminate early when the deeper node reaches the level of the shallower node, accurately measuring the distance between them plus one for the temporary edge. |
| Maximum number of queries in the queries array, which can be up to 10 to the 5th power | The solution must process each query in logarithmic time proportional to n to avoid exceeding time limit constraints. |
| Nodes located on extreme opposite sides of the complete binary tree at the maximum depth | Bit manipulation or standard binary tree parent division must be used to trace paths up to the lowest common ancestor efficiently without exploring unvisited branches. |