pathTo function

List<FNode>? pathTo(
  1. FNode root,
  2. FNode target
)

Returns the path from the tree root to target as a list of nodes (including root and target). Returns null if not found.

Implementation

List<FNode>? pathTo(FNode root, FNode target) {
  if (root.id == target.id) return [root];
  for (final child in childrenOf(root)) {
    final sub = pathTo(child, target);
    if (sub != null) return [root, ...sub];
  }
  return null;
}