java.lang.Object
g2501_2600.s2509_cycle_length_queries_in_a_tree.Solution

public class Solution extends Object
2509 - Cycle Length Queries in a Tree.<p>Hard</p> <p>You are given an integer <code>n</code>. There is a <strong>complete binary tree</strong> with <code>2<sup>n</sup> - 1</code> nodes. The root of that tree is the node with the value <code>1</code>, and every node with a value <code>val</code> in the range <code>[1, 2<sup>n - 1</sup> - 1]</code> has two children where:</p> <ul> <li>The left node has the value <code>2 * val</code>, and</li> <li>The right node has the value <code>2 * val + 1</code>.</li> </ul> <p>You are also given a 2D integer array <code>queries</code> of length <code>m</code>, where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>. For each query, solve the following problem:</p> <ol> <li>Add an edge between the nodes with values <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</li> <li>Find the length of the cycle in the graph.</li> <li>Remove the added edge between nodes with values <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</li> </ol> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>cycle</strong> is a path that starts and ends at the same node, and each edge in the path is visited only once.</li> <li>The length of a cycle is the number of edges visited in the cycle.</li> <li>There could be multiple edges between two nodes in the tree after adding the edge of the query.</li> </ul> <p>Return <em>an array</em> <code>answer</code> <em>of length</em> <code>m</code> <em>where</em> <code>answer[i]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/10/25/bexample1.png" alt="" /></p> <p><strong>Input:</strong> n = 3, queries = [[5,3],[4,7],[2,3]]</p> <p><strong>Output:</strong> [4,5,3]</p> <p><strong>Explanation:</strong> The diagrams above show the tree of 2<sup>3</sup> - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.</p> <ul> <li> <p>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.</p> </li> <li> <p>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.</p> </li> <li> <p>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.</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/10/25/aexample2.png" alt="" /></p> <p><strong>Input:</strong> n = 2, queries = [[1,2]]</p> <p><strong>Output:</strong> [2]</p> <p><strong>Explanation:</strong> The diagram above shows the tree of 2<sup>2</sup> - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.</p> <ul> <li>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.</li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 30</code></li> <li><code>m == queries.length</code></li> <li><code>1 <= m <= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>1 <= a<sub>i</sub>, b<sub>i</sub> <= 2<sup>n</sup> - 1</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • cycleLengthQueries

      public int[] cycleLengthQueries(int n, int[][] queries)