Find a node's parent and its path to the root in a tree given as undirected edges

Quick Overview

Given a general tree with nodes labeled 0 to n-1, rooted at node 0 and described by an undirected edge list, return a target node's parent and the path from that node up to the root. It tests building an adjacency list, recovering parent links with a graph traversal, and handling the root case.

Find a node's parent and its path to the root in a tree given as undirected edges

Company: Revolut

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a general tree (not necessarily binary) with `n` nodes labeled from `0` to `n - 1`, rooted at node `0`, and represented by an undirected edge list `edges`, find the parent of a given `target` node and the path from the `target` node back to the root. Return a list containing two elements: the parent of the `target` node (or `-1` if the target is the root), and the path from the `target` to the root as a list of node labels (inclusive of both the target and the root). The tree is guaranteed to be connected and have exactly `n - 1` edges. ### Function Signature ```python def tree_parent_search(n: int, edges: list[list[int]], target: int) -> list: ``` ### Rules - Each edge `[u, v]` is undirected. Its endpoints can appear in either order, so `[2, 0]` and `[0, 2]` describe the same edge. - The return value is `[parent, path]`. `parent` is an integer, and `path` is a list that starts at `target` and ends at `0`. - If `target` is `0`, the answer is `[-1, [0]]`. - A tree has exactly one path between any two nodes, so the answer is unique. ### Constraints - `1 <= n <= 10^5` - `0 <= target < n` - The tree has exactly `n` nodes labeled `0` to `n - 1`. - `edges.length == n - 1` - The tree is rooted at node `0`. ### Examples ```text Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], target = 5 Output: [2,[5,2,0]] ``` Node 0 has children 1 and 2, and node 2 has children 5 and 6. The parent of node 5 is node 2, and the path from node 5 to the root is [5, 2, 0]. ```text Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]], target = 4 Output: [3,[4,3,2,1,0]] ``` This is a linear chain 0, 1, 2, 3, 4. The parent of node 4 is node 3, and the path from node 4 to the root is [4, 3, 2, 1, 0]. ```text Input: n = 4, edges = [[1,0],[3,1],[1,2]], target = 3 Output: [1,[3,1,0]] ``` The edges are listed child-first here. Node 1 is the only neighbor of 0, so it is a child of the root, and 2 and 3 are children of 1. The parent of 3 is 1, and the path is [3, 1, 0].

Overview: Given a general tree with nodes labeled 0 to n-1, rooted at node 0 and described by an undirected edge list, return a target node's parent and the path from that node up to the root. It tests building an adjacency list, recovering parent links with a graph traversal, and handling the root case.

Revolut logo
Revolut
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a general tree (not necessarily binary) with n nodes labeled from 0 to n - 1, rooted at node 0, and represented by an undirected edge list edges, find the parent of a given target node and the path from the target node back to the root.

Return a list containing two elements: the parent of the target node (or -1 if the target is the root), and the path from the target to the root as a list of node labels (inclusive of both the target and the root).

The tree is guaranteed to be connected and have exactly n - 1 edges.

Function Signature

def tree_parent_search(n: int, edges: list[list[int]], target: int) -> list:

Rules

  • Each edge [u, v] is undirected. Its endpoints can appear in either order, so [2, 0] and [0, 2] describe the same edge.
  • The return value is [parent, path] . parent is an integer, and path is a list that starts at target and ends at 0 .
  • If target is 0 , the answer is [-1, [0]] .
  • A tree has exactly one path between any two nodes, so the answer is unique.

Constraints

  • 1 <= n <= 10^5
  • 0 <= target < n
  • The tree has exactly n nodes labeled 0 to n - 1 .
  • edges.length == n - 1
  • The tree is rooted at node 0 .

Examples

Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], target = 5
Output: [2,[5,2,0]]

Node 0 has children 1 and 2, and node 2 has children 5 and 6. The parent of node 5 is node 2, and the path from node 5 to the root is [5, 2, 0].

Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]], target = 4
Output: [3,[4,3,2,1,0]]

This is a linear chain 0, 1, 2, 3, 4. The parent of node 4 is node 3, and the path from node 4 to the root is [4, 3, 2, 1, 0].

Input: n = 4, edges = [[1,0],[3,1],[1,2]], target = 3
Output: [1,[3,1,0]]

The edges are listed child-first here. Node 1 is the only neighbor of 0, so it is a child of the root, and 2 and 3 are children of 1. The parent of 3 is 1, and the path is [3, 1, 0].