> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/array/matrix/1424.-diagonal-traverse-ii.md).

# 1424. Diagonal Traverse II

## Problem Statement

<br>

Given a 2D integer array `nums`, return *all elements of* `nums` *in diagonal order as shown in the below images*.

&#x20;

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png)

<pre><code><strong>Input: nums = [[1,2,3],[4,5,6],[7,8,9]]
</strong><strong>Output: [1,4,2,7,5,3,8,6,9]
</strong></code></pre>

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/04/08/sample_2_1784.png)

<pre><code><strong>Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
</strong><strong>Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= nums.length <= 105`
* `1 <= nums[i].length <= 105`
* `1 <= sum(nums[i].length) <= 105`
* `1 <= nums[i][j] <= 105`

## Intuition

```
Approach:1

Note that in forward daigonal the sum of i+j at particular daigonal is same
We can use this, Maintain  a map for this
(If we want backward daigonal then difference of i+j)

Tc =O(N) SC = O(N)

Approach: 2
BFS kind

Push the element to the left and bottom at each place
TC - O(N) ; SC = O(root(N)) Daigonal in the queue in worst case
```

### Links

<https://leetcode.com/problems/diagonal-traverse-ii/description/?envType=daily-question&envId=2023-11-22>

### Video Links

<https://www.youtube.com/watch?v=5hG2nDEiwlE&ab_channel=AryanMittal>

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
        int maxKey=0;
        vector<int> ans;
        unordered_map<int, vector<int>> mp;

        for(int i=nums.size()-1; i>=0; i--){
            for(int j=0; j<nums[i].size(); j++){
                mp[i+j].push_back(nums[i][j]);
                maxKey = max(maxKey, i+j);
            }
        }

        for(int i=0; i<=maxKey; i++){
            for(auto &it: mp[i]){
                ans.push_back(it);
            }
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
        int n = nums.size();
        queue<pair<int,int>> q;
        vector<int> ans;
        q.push({0,0});

        while(!q.empty()){
            int size = q.size();

            for(int i=0; i<size; i++){
                int row = q.front().first;
                int col = q.front().second;
                ans.push_back(nums[row][col]);
                q.pop();

                if(col==0 and row+1<n){
                    q.push({row+1,col});
                }

                if(col+1 < nums[row].size()){
                    q.push({row,col+1});
                }
            }
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/array/matrix/1424.-diagonal-traverse-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
