Tree/level_order_traversal library
🌳 Level Order Traversal (BFS)
Traverses a binary tree level by level, from left to right. Returns a list of lists, where each inner list represents one level.
Time complexity: O(n) where n is the number of nodes Space complexity: O(w) where w is the maximum width of the tree
Example:
final root = BinaryTreeNode<int>(10);
root.left = BinaryTreeNode<int>(5);
root.right = BinaryTreeNode<int>(15);
root.left!.left = BinaryTreeNode<int>(3);
root.left!.right = BinaryTreeNode<int>(7);
final result = levelOrderTraversal(root);
// result: [[10], [5, 15], [3, 7]]
Functions
-
levelOrderTraversal<
T> (BinaryTreeNode< T> ? root) → List<List< T> >