# 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-left to bottom-right. 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 [1,5,3,2]
   - Case 2: 3×3 matrix with input [[3,2,5],[6,7,0],[1,3,9]] and output [5,2,0,3,7,9,6,3,1]
2. Grey diagonal arrows indicate the traversal direction
3. Each diagonal moves from top-left to bottom-right
4. The diagonals are parallel to each other
5. Multiple diagonals are processed from right to left

# Visual Patterns
1. Traversal Order Pattern:
   - Start with the rightmost diagonal
   - Process each diagonal from top to bottom
   - Move to the next diagonal on the left
   - 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-right corner (0, len(matrix[0])-1)
   - Each subsequent diagonal starts one column to the left on the top row

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-right to bottom-left
    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 (n - 1 - d + i) >= 0:
                    result.append(matrix[i][n - 1 - d + i])
        else:
            # Lower half excluding main diagonal
            for i in range(d - n + 1, n):
                if i < n and (n - 1 - d + i) >= 0:
                    result.append(matrix[i][n - 1 - d + i])

    return result


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

    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`:\n" + '\n'.join([str(row) for row in 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`:\n" + '\n'.join([str(row) for row in matrix]) + "\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()