# Problem Description
This is a matrix traversal problem where we need to traverse an n×n matrix following diagonal paths from top to bottom, with each diagonal moving from top-right to bottom-left. The traversal needs to be done in a specific order to produce the output sequence shown in the examples.

# Visual Facts
1. Two example cases are provided:
   - Case 1: 2×2 matrix with input [[5,1],[2,3]] and output [5,1,2,3]
   - Case 2: 3×3 matrix with input [[3,2,5],[6,7,0],[1,3,9]] and output [3,2,6,5,7,1,0,3,9]
2. Grey diagonal arrows indicate the traversal direction
3. Each diagonal moves from top-right to bottom-left
4. The diagonals are parallel to each other
5. Multiple diagonals are processed from left to right

# Visual Patterns
1. Traversal Order Pattern:
   - Start with the leftmost diagonal
   - Process each diagonal from top to bottom
   - Move to the next diagonal on the right
   - Continue until all diagonals are processed

2. Diagonal Formation Pattern:
   - Each diagonal has a constant sum of row+column indices
   - First diagonal starts from top-left corner (0,0)
   - Each subsequent diagonal starts one column to the right on the top row
   - Diagonal elements share the same row+column sum

3. Mathematical Pattern:
   - For an n×n matrix, there are (2n-1) diagonals
   - Each diagonal moves in the direction of (row+1, col-1)

# Solution
from typing import List

def solution(matrix: List[List[int]]) -> List[int]:
    """
    Traverses an n x n matrix according to a specified pattern.

    Parameters:
        matrix (List[List[int]]): An n x n integer matrix      

    Returns:
        List[int]: A list containing elements in the order of traversal
    """
    n = len(matrix)
    result = []

    # Traverse diagonals from top-left to bottom-right
    for d in range(2 * n - 1):
        if d < n:
            # Upper half including main diagonal
            for i in range(d + 1):
                if i < n and (d - i) < n:
                    result.append(matrix[i][d - i])
        else:
            # Lower half excluding main diagonal
            for i in range(d - n + 1, n):
                if i < n and (d - i) < n:
                    result.append(matrix[i][d - i])

    return result


# Test Script
def test():
    test_cases = [
        ([[1]], [1]),
        ([[1, 4], [2, 6]], [1, 4, 2, 6]),
        ([[45, 72, 13], [34, 9, 87], [90, 31, 56]], [45, 72, 34, 13, 9, 90, 87, 31, 56]),
        ([[84, 57, 62], [41, 92, 11], [70, 15, 28]], [84, 57, 41, 62, 92, 70, 11, 15, 28]),
        ([[3, 1, 2, 2], [6, 1, 8, 0], [2, 3, 4, 1], [4, 8, 0, 9]], [3, 1, 6, 2, 1, 2, 2, 8, 3, 4, 0, 4, 8, 1, 0, 9]),
        ([[42, 17, 85, 33], [54, 76, 9, 91], [63, 47, 28, 10], [12, 99, 57, 31]], [42, 17, 54, 85, 76, 63, 33, 9, 47, 12, 91, 28, 99, 10, 57, 31]),
        ([[50, 42, 91, 28], [83, 55, 12, 73], [17, 60, 47, 10], [99, 64, 34, 6]], [50, 42, 83, 91, 55, 17, 28, 12, 60, 99, 73, 47, 64, 10, 34, 6]),
        ([[39, 99, 61, 56, 49], [62, 70, 30, 26, 33], [43, 85, 12, 92, 72], [20, 44, 4, 40, 21], [18, 73, 81, 78, 57]], [39, 99, 62, 61, 70, 43, 56, 30, 85, 20, 49, 26, 12, 44, 18, 33, 92, 4, 73, 72, 40, 81, 21, 78, 57]),
        ([[87, 15, 2, 10, 41], [25, 31, 67, 88, 50], [60, 40, 37, 7, 13], [72, 89, 75, 42, 92], [19, 65, 32, 48, 24]], [87, 15, 25, 2, 31, 60, 10, 67, 40, 72, 41, 88, 37, 89, 19, 50, 7, 75, 65, 13, 42, 32, 92, 48, 24]),
        ([[51, 4, 18, 52, 79], [85, 90, 6, 3, 22], [99, 82, 95, 93, 8], [71, 94, 87, 98, 62], [1, 49, 38, 77, 43]], [51, 4, 85, 18, 90, 99, 52, 6, 82, 71, 79, 3, 95, 94, 1, 22, 93, 87, 49, 8, 98, 38, 62, 77, 43])
    ]

    for matrix, expected_output in test_cases:
        try:
            output = solution(matrix)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`matrix`: {matrix}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

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

test()