# Problem Description
This is a matrix traversal problem where we need to traverse an n×n matrix following diagonal paths from bottom to top, with each diagonal moving from bottom-left to top-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 [3,2,1,5]
   - Case 2: 3×3 matrix with input [[3,2,5],[6,7,0],[1,3,9]] and output [9,3,0,1,7,5,6,2,3]
2. Grey diagonal arrows indicate the traversal direction
3. Each diagonal moves from bottom-left to top-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 bottom to top
   - Move to the next diagonal on the left
   - Continue until all diagonals are processed

2. Diagonal Formation Pattern:
   - Every diagonal start from a basic position. The first basic position is at (len(matrix)-1,len(matrix[0])-1). Every time after iterate all the elements of this diagonal, it wiil change the basic position.
   - The basic position are the elements of the last row and first column of the matrix, traversing from the last column of the last row to the elements of the first column of the last row, and then up to the first row and first column.
   - Start from a basic position, the direction of diagonal is (row-1, col+1). Iterate all the elements of this diagonal and then repeat this operation with next basic position.


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

    return result


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

    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()