printAllRootToLeafPaths<T> function

List<List<T>> printAllRootToLeafPaths<T>(
  1. BinaryTreeNode<T>? root
)

Implementation

List<List<T>> printAllRootToLeafPaths<T>(BinaryTreeNode<T>? root) {
  final List<List<T>> paths = [];
  void dfs(BinaryTreeNode<T>? node, List<T> path) {
    if (node == null) return;
    path.add(node.value);
    if (node.left == null && node.right == null) {
      paths.add(List<T>.from(path));
    } else {
      dfs(node.left, path);
      dfs(node.right, path);
    }
    path.removeLast();
  }

  dfs(root, []);
  return paths;
}