build_graph
to construct the graph representation of the tree, connecting each node to its children and parent.graph
to store the adjacency list of each node.visited
to keep track of visited nodes during BFS.queue
for BFS, starting with the target node at distance 0.k
, add the node's value to the result list.k
are processed.