<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width: 158px; height: 143px;" />
<pre>
<strong>Input:</strong> nums = [[1,2,3],[4,5,6],[7,8,9]]
<strong>Output:</strong> [1,4,2,7,5,3,8,6,9]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_2_1784.png" style="width: 230px; height: 177px;" />
<pre>
<strong>Input:</strong> nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
<strong>Output:</strong> [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i].length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= sum(nums[i].length) &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
        vector<int> ans ;
        queue<pair<int, int>> st ;
        vector<vector<int>> vis(nums.size()) ; 
        for(int i = 0 ; i < nums.size() ; i++) vis[i].resize(nums[i].size() , 0) ; 
        pair<int ,int > start  = {0 , 0} ; 
        st.push({0, 0}) ;
        vis[0][0] = 1 ; 
        // ans.push_back(nums[0][0]) ;
        while(!st.empty()){
            int size = st.size() ;
            for(int i = 0 ; i < size ; i++){
                pair<int ,int> cur = st.front() ;
                st.pop() ; 
                cout << nums[cur.first][cur.second] << endl ;
                ans.push_back(nums[cur.first][cur.second]) ;
                pair<int ,int>  nei_d = {cur.first + 1 , cur.second} ;
                pair<int ,int>  nei_r = {cur.first , cur.second + 1 };
                if(nei_d.first >= 0 && nei_d.first < nums.size() && nei_d.second >= 0 && nei_d.second < vis[nei_d.first].size() && !vis[nei_d.first][nei_d.second]){
                    st.push(nei_d) ;
                    vis[nei_d.first][nei_d.second] = 1 ;
                }
                if(nei_r.first >= 0 && nei_r.first < nums.size() && nei_r.second >= 0 && nei_r.second < vis[nei_r.first].size() && !vis[nei_r.first][nei_r.second]){
                    st.push(nei_r) ;
                    vis[nei_r.first][nei_r.second] = 1 ;
                }
            }
    
        }
        return ans ;
    }
};
80238