<p>Given an <code>m x n</code> matrix <code>mat</code>, return <em>an array of all the elements of the array in a diagonal order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/diag1-grid.jpg" style="width: 334px; height: 334px;" />
<pre>
<strong>Input:</strong> mat = [[1,2,3],[4,5,6],[7,8,9]]
<strong>Output:</strong> [1,2,4,7,5,3,6,8,9]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> mat = [[1,2],[3,4]]
<strong>Output:</strong> [1,2,3,4]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= mat[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
        if (mat.empty()) return {}; // Handle empty matrix case

        int m = mat.size();
        int n = mat[0].size();
        vector<int> result; // Initialize result vector without a specific size
        vector<vector<bool>> visited(m, vector<bool>(n, false)); // Visited array

        queue<pair<int, int>> q; // Queue for BFS
        q.push({0, 0}); // Start from the top-left corner
        visited[0][0] = true;

        bool reverse = false; // Flag to determine the direction

        while (!q.empty()) {
            int size = q.size();
            vector<int> level; // Temporary vector to store the current diagonal elements
            
            for (int i = 0; i < size; i++) {
                auto cur = q.front();
                q.pop();
                int x = cur.first;
                int y = cur.second;

                // Add the current element to the level vector
                if (!reverse) {
                    level.push_back(mat[x][y]); // Append to the end for normal order
                } else {
                    level.insert(level.begin(), mat[x][y]); // Insert at the beginning for reverse order
                }

                // Check and enqueue the valid neighbors
                if (isValid(x + 1, y, m, n, visited)) {
                    q.push({x + 1, y});
                    visited[x + 1][y] = true;
                }
                if (isValid(x, y + 1, m, n, visited)) {
                    q.push({x, y + 1});
                    visited[x][y + 1] = true;
                }
            }
            // Append the current diagonal to the result
            result.insert(result.end(), level.begin(), level.end());
            // After processing the current diagonal, toggle the reverse flag
            reverse = !reverse;
        }

        return result; // Return the final diagonal order
    }

private:
    bool isValid(int x, int y, int m, int n, vector<vector<bool>>& visited) {
        return x >= 0 && x < m && y >= 0 && y < n && !visited[x][y]; // Check bounds and visited
    }
};
27256