Find the root-to-node path in an N-ary tree given as an edge list

Quick Overview

Given an N-ary tree described by a root value and a list of parent-child edges, return the node values on the path from the root to a target node, or an empty list when the target is missing. It tests turning an edge list into a usable tree structure, linear-time traversal, and handling of the root and missing-target cases.

Find the root-to-node path in an N-ary tree given as an edge list

Company: Tesla

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an N-ary tree as a root value and a list of `edges`. Each edge `[parent, child]` connects a node to one of its children, and every node has a unique value. You are also given a `target` value. Return the path from the root to the target node as a list of node values, starting with the root and ending with the target. If the target is not in the tree, return an empty list. ### Function Signature ```python def path_from_root(root: int, edges: list[list[int]], target: int) -> list[int]: ``` ### Rules - Each edge is `[parent, child]`. The edges form one tree rooted at `root`: every node other than the root appears as the child in exactly one edge, and the root never appears as a child. - A tree with a single node has `edges = []`. - The path includes both endpoints. If `target == root`, the answer is `[root]`. - Values are unique and a tree has exactly one path from the root to each node, so the answer is unique. The order of the edges in the input does not change it. ### Constraints - `1 <= number of nodes <= 10^4` - `len(edges) == number of nodes - 1` - `1 <= node value <= 10^5` - All node values are unique. - The target value may or may not exist in the tree. ### Examples ```text Input: root = 1, edges = [[1,2],[1,3],[1,4],[2,5],[2,6],[4,7],[4,8],[7,9]], target = 9 Output: [1,4,7,9] ``` The root 1 has children 2, 3 and 4. Node 4 has children 7 and 8, and node 7 has the single child 9, so the path is 1, 4, 7, 9. ```text Input: root = 1, edges = [[1,2],[1,3],[1,4],[2,5],[2,6],[4,7],[4,8],[7,9]], target = 6 Output: [1,2,6] ``` Same tree. Node 6 is a child of 2, and 2 is a child of the root. ```text Input: root = 1, edges = [[1,2],[1,3]], target = 4 Output: [] ``` The tree holds only the values 1, 2 and 3, so there is no node with value 4.

Overview: Given an N-ary tree described by a root value and a list of parent-child edges, return the node values on the path from the root to a target node, or an empty list when the target is missing. It tests turning an edge list into a usable tree structure, linear-time traversal, and handling of the root and missing-target cases.

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

You are given an N-ary tree as a root value and a list of edges. Each edge [parent, child] connects a node to one of its children, and every node has a unique value. You are also given a target value.

Return the path from the root to the target node as a list of node values, starting with the root and ending with the target. If the target is not in the tree, return an empty list.

Function Signature

def path_from_root(root: int, edges: list[list[int]], target: int) -> list[int]:

Rules

  • Each edge is [parent, child] . The edges form one tree rooted at root : every node other than the root appears as the child in exactly one edge, and the root never appears as a child.
  • A tree with a single node has edges = [] .
  • The path includes both endpoints. If target == root , the answer is [root] .
  • Values are unique and a tree has exactly one path from the root to each node, so the answer is unique. The order of the edges in the input does not change it.

Constraints

  • 1 <= number of nodes <= 10^4
  • len(edges) == number of nodes - 1
  • 1 <= node value <= 10^5
  • All node values are unique.
  • The target value may or may not exist in the tree.

Examples

Input: root = 1, edges = [[1,2],[1,3],[1,4],[2,5],[2,6],[4,7],[4,8],[7,9]], target = 9
Output: [1,4,7,9]

The root 1 has children 2, 3 and 4. Node 4 has children 7 and 8, and node 7 has the single child 9, so the path is 1, 4, 7, 9.

Input: root = 1, edges = [[1,2],[1,3],[1,4],[2,5],[2,6],[4,7],[4,8],[7,9]], target = 6
Output: [1,2,6]

Same tree. Node 6 is a child of 2, and 2 is a child of the root.

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

The tree holds only the values 1, 2 and 3, so there is no node with value 4.