Browse curriculum

N-Queens (LeetCode 51)

N-Queens (LeetCode 51): place n queens on an n × n board so none attack each other with backtracking: one queen per row, and sets for columns and both diagonals.

Problem Understanding

N-Queens: place n queens on an n × n chessboard so that no two attack each other (no shared row, column or diagonal). Return every distinct board, written as strings with Q for a queen and . for an empty square.

Example: n = 4 has 2 solutions: [".Q..", "...Q", "Q...", "..Q."] and its mirror image.

The Intuition: One Queen per Row, Three Sets

Every solution has exactly one queen in each row, so decide rows one at a time with backtracking: for row r, choose a column.

A column choice is safe only if no earlier queen shares:

  • its column c,
  • its diagonal, where every square has the same r − c,
  • its anti-diagonal, where every square has the same r + c.

Three sets answer all three questions in O(1). Place a safe queen, mark its three values, recurse into the next row, then unmark them and try the next column. It is the same choose / explore / un-choose template as Permutations, with pruning that cuts most branches early.

N-Queens Visualization

EMPTY BOARD

board (row 0 at the top)

solutions (0) · column per row

None yet

 

Place 4 queens, one per row, so that none share a column or a diagonal

Next

Row 0, column 0 is safe: place a queen and move to row 1

Free preview: the first 24 of 80 steps

n

Queens are drawn as ♛ and every square they attack is shaded, so a blocked try always lands on a shaded square. Each solution is listed beside the board as the queen's column in every row. Try n = 3 to watch every branch dead-end, and n = 5 for 10 solutions.

N-Queens Dry Run

Input: n = 4 (columns 0–3)

Row Tried Result
0 0 place
1 0, 1 blocked; 2 place
2 0–3 all blocked backtrack to row 1
1 3 place
2 1 place
3 0–3 all blocked backtrack to row 0
0 1 place, then 3, 0, 2: solution [1, 3, 0, 2]
0 2 mirror image: solution [2, 0, 3, 1]

Answer: 2 boards.

N-Queens LeetCode Solution

Row-by-row backtracking with column and diagonal sets. Java, C++ and C index diagonals as r − c + n so they stay non-negative.

N-Queens Code
1var solveNQueens = function(n) {
2 const res = [], queens = [];
3 const cols = new Set(), diag = new Set(), anti = new Set();
4 const place = (r) => {
5 if (r === n) {
6 res.push(queens.map((c) => '.'.repeat(c) + 'Q' + '.'.repeat(n - c - 1)));
7 return;
8 }
9 for (let c = 0; c < n; c++) {
10 if (cols.has(c) || diag.has(r - c) || anti.has(r + c)) continue;
11 cols.add(c); diag.add(r - c); anti.add(r + c); queens.push(c);
12 place(r + 1);
13 cols.delete(c); diag.delete(r - c); anti.delete(r + c); queens.pop();
14 }
15 };
16 place(0);
17 return res;
18};

N-Queens in Python, Java, C++, TypeScript and C

N-Queens in Python
class Solution:
    def solveNQueens(self, n: int) -> List[List[str]]:
        res, queens = [], []
        cols, diag, anti = set(), set(), set()
        def place(r):
            if r == n:
                res.append(['.' * c + 'Q' + '.' * (n - c - 1) for c in queens])
                return
            for c in range(n):
                if c in cols or r - c in diag or r + c in anti:
                    continue
                cols.add(c); diag.add(r - c); anti.add(r + c); queens.append(c)
                place(r + 1)
                cols.remove(c); diag.remove(r - c); anti.remove(r + c); queens.pop()
        place(0)
        return res
N-Queens in Java
class Solution {
    public List<List<String>> solveNQueens(int n) {
        List<List<String>> res = new ArrayList<>();
        place(0, n, new int[n], new boolean[n], new boolean[2 * n], new boolean[2 * n], res);
        return res;
    }

    private void place(int r, int n, int[] queens, boolean[] cols, boolean[] diag, boolean[] anti, List<List<String>> res) {
        if (r == n) {
            List<String> board = new ArrayList<>();
            for (int c : queens) board.add(".".repeat(c) + "Q" + ".".repeat(n - c - 1));
            res.add(board);
            return;
        }
        for (int c = 0; c < n; c++) {
            if (cols[c] || diag[r - c + n] || anti[r + c]) continue;
            cols[c] = diag[r - c + n] = anti[r + c] = true;
            queens[r] = c;
            place(r + 1, n, queens, cols, diag, anti, res);
            cols[c] = diag[r - c + n] = anti[r + c] = false;
        }
    }
}
N-Queens in C++
class Solution {
public:
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<string>> res;
        vector<string> board(n, string(n, '.'));
        vector<bool> cols(n), diag(2 * n), anti(2 * n);
        place(0, n, board, cols, diag, anti, res);
        return res;
    }

private:
    void place(int r, int n, vector<string>& board, vector<bool>& cols, vector<bool>& diag, vector<bool>& anti, vector<vector<string>>& res) {
        if (r == n) {
            res.push_back(board);
            return;
        }
        for (int c = 0; c < n; c++) {
            if (cols[c] || diag[r - c + n] || anti[r + c]) continue;
            cols[c] = diag[r - c + n] = anti[r + c] = true;
            board[r][c] = 'Q';
            place(r + 1, n, board, cols, diag, anti, res);
            board[r][c] = '.';
            cols[c] = diag[r - c + n] = anti[r + c] = false;
        }
    }
};
N-Queens in TypeScript
function solveNQueens(n: number): string[][] {
    const res: string[][] = [], queens: number[] = [];
    const cols = new Set<number>(), diag = new Set<number>(), anti = new Set<number>();
    const place = (r: number): void => {
        if (r === n) {
            res.push(queens.map((c) => '.'.repeat(c) + 'Q' + '.'.repeat(n - c - 1)));
            return;
        }
        for (let c = 0; c < n; c++) {
            if (cols.has(c) || diag.has(r - c) || anti.has(r + c)) continue;
            cols.add(c); diag.add(r - c); anti.add(r + c); queens.push(c);
            place(r + 1);
            cols.delete(c); diag.delete(r - c); anti.delete(r + c); queens.pop();
        }
    };
    place(0);
    return res;
}
N-Queens in C
void place(int r, int n, int* queens, bool* cols, bool* diag, bool* anti, char*** res, int* count) {
    if (r == n) {
        res[*count] = malloc(n * sizeof(char*));
        for (int i = 0; i < n; i++) {
            res[*count][i] = malloc(n + 1);
            memset(res[*count][i], '.', n);
            res[*count][i][queens[i]] = 'Q';
            res[*count][i][n] = '\0';
        }
        (*count)++;
        return;
    }
    for (int c = 0; c < n; c++) {
        if (cols[c] || diag[r - c + n] || anti[r + c]) continue;
        cols[c] = diag[r - c + n] = anti[r + c] = true;
        queens[r] = c;
        place(r + 1, n, queens, cols, diag, anti, res, count);
        cols[c] = diag[r - c + n] = anti[r + c] = false;
    }
}

char*** solveNQueens(int n, int* returnSize, int** returnColumnSizes) {
    char*** res = malloc(400 * sizeof(char**));
    int queens[9];
    bool cols[9] = {false}, diag[18] = {false}, anti[18] = {false};
    *returnSize = 0;
    place(0, n, queens, cols, diag, anti, res, returnSize);
    *returnColumnSizes = malloc(*returnSize * sizeof(int));
    for (int i = 0; i < *returnSize; i++) (*returnColumnSizes)[i] = n;
    return res;
}

Edge Cases & Common Mistakes

  • n = 2 or 3: there is no solution; return an empty list.
  • Negative diagonal index: r − c can be negative; offset it by n when using arrays.
  • Forgetting to unmark: remove the column and both diagonals after the recursive call, or later branches see phantom queens.
  • Checking the whole board: scanning all earlier queens works but costs O(n) per check; the three sets make it O(1).
  • N-Queens II (LeetCode 52): the same search, counting solutions instead of building boards.

Summary

Place one queen per row. For each column, skip it if its column, r − c diagonal or r + c anti-diagonal is taken; otherwise mark all three, recurse to the next row, and unmark on the way back. Reaching row n records a board. O(n!) time, O(n) extra space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Go row by row

    Start at row 0 with empty column, diagonal (r − c) and anti-diagonal (r + c) sets.

  2. Try every safe column

    For each column whose column, r − c and r + c are all free, place a queen, add them to the sets, and recurse to the next row.

  3. Backtrack and record

    After the recursive call, remove the queen and its marks. When the row index reaches n, record the board.

Frequently asked questions

How do you check whether a square is attacked in N-Queens?

Since there is one queen per row, only columns and diagonals can clash. Squares on the same diagonal share r − c, and squares on the same anti-diagonal share r + c. Keep three sets (columns, r − c, r + c); a square is safe if none of them contains its value, an O(1) check.


Why place one queen per row?

Two queens in the same row always attack each other, so every solution has exactly one queen in each row. Placing row by row removes row conflicts entirely and turns the search into choosing a column for each row.


What is the time complexity of N-Queens?

The first row has n choices, the second at most n − 1, and so on, so the search is bounded by O(n!). Pruning with the diagonal sets cuts it far below that in practice; building each solution board adds O(n²).

New to the technique behind this problem? Watch it run step by step:

Backtracking Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description