findParent function

FNode? findParent(
  1. FNode root,
  2. FNode target
)

Finds the direct parent of target in the tree with root root. Returns null if target is the root or not found.

Implementation

FNode? findParent(FNode root, FNode target) {
  for (final child in childrenOf(root)) {
    if (child.id == target.id) return root;
    final found = findParent(child, target);
    if (found != null) return found;
  }
  return null;
}