input: Write a function to traverse a 2D matrix in a spiral order.
  assert spiralTraverse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == [1, 2, 3, 6, 9, 8, 7, 4, 5]
  assert spiralTraverse([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) == [1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]
  assert spiralTraverse([[1]]) == [1]
  assert spiralTraverse([[1, 2], [3, 4]]) == [1, 2, 4, 3]
  assert spiralTraverse([[1, 2, 3], [4, 5, 6]]) == [1, 2, 3, 6, 5, 4]
output: def spiralTraverse(matrix):
      result = []
      if not matrix:
          return result
  
      top, bottom = 0, len(matrix) - 1
      left, right = 0, len(matrix[0]) - 1
  
      while top <= bottom and left <= right:
          for i in range(left, right + 1):
              result.append(matrix[top][i])
          top += 1
  
          for i in range(top, bottom + 1):
              result.append(matrix[right][i])
          right -= 1
  
          if top <= bottom:
              for i in range(right, left - 1, -1):
                  result.append(matrix[bottom][i])
              bottom -= 1
  
          if left <= right:
              for i in range(bottom, top - 1, -1):
                  result.append(matrix[i][left])
              left += 1
  
      return result