Tree/flatten_binary_tree_to_linked_list library
🌳 Flatten Binary Tree to Linked List (in-place, preorder)
Flattens a binary tree to a linked list in preorder traversal order. The right child points to the next node, and left is always null.
Time complexity: O(n) Space complexity: O(1) (in-place)
Example:
final root = BinaryTreeNode<int>(1);
root.left = BinaryTreeNode<int>(2);
root.right = BinaryTreeNode<int>(5);
root.left!.left = BinaryTreeNode<int>(3);
root.left!.right = BinaryTreeNode<int>(4);
root.right!.right = BinaryTreeNode<int>(6);
flattenBinaryTreeToLinkedList(root);
// root is now a right-skewed linked list: 1->2->3->4->5->6
Functions
-
flattenBinaryTreeToLinkedList<
T> (BinaryTreeNode< T> ? root) → void