N-Queens problem can be solved with a recursive function that contains a loop. At each level, we try to place all the queens that we can in the current row, and then we recurse on the following row.

def dfs(row,sol)
	if row == SIZE:
	   sols.append(sol[:])
	for i in range(SIZE):
	    diag1 = row - i
	    diag2 = row + i
	    if i not in sol and diag1 not in diag1_set and diag2 not in diag2_set:
		    diag1_set.add(diag1)
		    diag2_set.add(diag2)
		    sol.append(i)
		    dfs(i+1, sol)
		    sol.pop()
		    diag1_set.remove(diag1)
		    diag2_set.remove(diag2)