Tree/zigzag_traversal library
🌳 Zigzag Level Order Traversal
Traverses a binary tree level by level, alternating between left-to-right and right-to-left order for each 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);
root.right!.left = BinaryTreeNode<int>(12);
final result = zigzagTraversal(root);
// result: [[10], [15, 5], [3, 7, 12]]
Functions
-
zigzagTraversal<
T> (BinaryTreeNode< T> ? root) → List<List< T> >