# Problem Description
The problem requires implementing a function that processes a binary tree and returns a sequence of node values based on specific rules. The function takes a root node of a tree as input and should return a list of integers representing the "leftmost" nodes in the tree, following certain patterns visible in the examples.

# Visual Facts
1. Each example shows a binary tree with numbered nodes (1 to 8 maximum).
2. Some nodes are highlighted in orange, while others are in black.
3. Each example has an associated output sequence shown below it.
4. Orange arrows point to the left side of each tree.
5. The trees have different shapes and depths:
   - Example 1: depth 4, 8 nodes.
   - Example 2: depth 4, 7 nodes.
   - Example 3: depth 4, 5 nodes.
6. The output always starts with node 1 (root).
7. Each tree is a valid binary tree.
8. The output sequence length varies (4 numbers in each example).

# Visual Patterns
Output Sequence Pattern:
- The output sequence consists of only the orange-highlighted nodes.
- The sequence follows top-to-bottom order of the highlighted nodes.
- The root node is always included in the output.
- The output sequence includes all the leftmost nodes at each level.

# Solution
from collections import deque

class TreeNode:
    """
    Represents a node in a tree. Each node contains an integer value and a list of child TreeNodes.
    
    Attributes:
        value (int): The integer value of the node.
        children (list): A list of child nodes (TreeNode objects).
    """
    def __init__(self, value: int, children: list=[]):
        self.value = value
        self.children = children

def solution(root: TreeNode) -> list[int]:
    """
    Computes the output sequence based on the given tree structure.

    Parameters:
        root (TreeNode): The root node of the tree.

    Returns:
        list[int]: A list of integers representing the output sequence as illustrated in the figure.
    """
    if not root:
        return []
    queue = deque([root])
    result = []
    while queue:
        level_size = len(queue)
        for i in range(level_size):
            node = queue.popleft()
            if i == 0:
                result.append(node.value)
            for child in node.children:
                queue.append(child)
    return result


# Test Script
def tree_to_string(node, level=0):
    """Helper function to convert a tree to a readable string format."""
    if not node:
        return "None"
    
    result = f"TreeNode({node.value})"
    if node.children:
        result += " → ["
        child_strings = []
        for child in node.children:
            child_strings.append(tree_to_string(child, level + 1))
        result += ", ".join(child_strings)
        result += "]"
    return result

def test():
    Tree0 = TreeNode(0)
    Tree1 = TreeNode(1, [TreeNode(1), TreeNode(2)])
    Tree2 = TreeNode(3, [TreeNode(3)])
    Tree3 = TreeNode(2, [TreeNode(2), TreeNode(2, [TreeNode(2)])])
    Tree4 = TreeNode(2, [TreeNode(1, [TreeNode(2)]), TreeNode(2, [TreeNode(3)])])
    Tree5 = TreeNode(5, [TreeNode(5, [TreeNode(5, [TreeNode(5)]), TreeNode(5)]), TreeNode(5, [TreeNode(5), TreeNode(5)])])
    Tree6 = TreeNode(8, [TreeNode(8, [TreeNode(8, [TreeNode(8)]), TreeNode(8)]), TreeNode(8, [TreeNode(8, [TreeNode(8)]), TreeNode(8)])])
    Tree7 = TreeNode(2, [TreeNode(2, [TreeNode(2, [TreeNode(1)]), TreeNode(2)]), TreeNode(2, [TreeNode(2), TreeNode(1, [TreeNode(2)])])])
    Tree8 = TreeNode(3, [TreeNode(3, [TreeNode(3, [TreeNode(3)]), TreeNode(3)]), TreeNode(3, [TreeNode(3), TreeNode(3, [TreeNode(3)])])])
    Tree9 = TreeNode(3, [TreeNode(3, [TreeNode(3, [TreeNode(3)]), TreeNode(8, [TreeNode(3)])]), TreeNode(3, [TreeNode(8), TreeNode(8, [TreeNode(3)])])])
    Tree10 = TreeNode(3, [TreeNode(3, [TreeNode(3, [TreeNode(3)]), TreeNode(1, [TreeNode(1)])]), TreeNode(3, [TreeNode(3, [TreeNode(1)]), TreeNode(3, [TreeNode(3)])])])

    test_cases = [
        (Tree0, [0]),
        (Tree1, [1, 1]),
        (Tree2, [3, 3]),
        (Tree3, [2, 2, 2]),
        (Tree4, [2, 1, 2]),
        (Tree5, [5, 5, 5, 5]),
        (Tree6, [8, 8, 8, 8]),
        (Tree7, [2, 2, 2, 1]),
        (Tree8, [3, 3, 3, 3]),
        (Tree9, [3, 3, 3, 3]),
        (Tree10, [3, 3, 3, 3]),
    ]
    for i, (root, expected_output) in enumerate(test_cases):
        try:
            output = solution(root)
        except Exception as e:
            error_message = (
                f"An exception occurred while running test case {i}:\n"
                f"`root`: {tree_to_string(root)}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case {i} failed:\n"
                f"`root`: {tree_to_string(root)}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()