Tree/tree_traversals library
🌳 Binary Tree Traversal Algorithms
Three fundamental tree traversal algorithms that visit all nodes in a binary tree in different orders.
- Inorder: Left -> Root -> Right (gives sorted order for BST)
- Preorder: Root -> Left -> Right (useful for copying trees)
- Postorder: Left -> Right -> Root (useful for deleting trees)
Time complexity: O(n) where n is the number of nodes Space complexity: O(h) where h is the height of the tree (worst case O(n))
Functions
-
inorderTraversal<
T> (BinaryTreeNode< T> ? root) → List<T> - Inorder Traversal: Left -> Root -> Right
-
postorderTraversal<
T> (BinaryTreeNode< T> ? root) → List<T> - Postorder Traversal: Left -> Right -> Root
-
preorderTraversal<
T> (BinaryTreeNode< T> ? root) → List<T> - Preorder Traversal: Root -> Left -> Right