walk<T> static method

void walk<T>(
  1. List<TreeNode<T>> roots,
  2. void visit(
    1. TreeNode<T> node,
    2. List<TreeNode<T>> ancestors
    )
)

Depth-first walk; visit receives each node and its ancestor path.

Implementation

static void walk<T>(List<TreeNode<T>> roots, void Function(TreeNode<T> node, List<TreeNode<T>> ancestors) visit) {
  void rec(List<TreeNode<T>> nodes, List<TreeNode<T>> path) {
    for (final n in nodes) {
      visit(n, path);
      if (n.children.isNotEmpty) rec(n.children, [...path, n]);
    }
  }

  rec(roots, const []);
}