def hamming_weight(n, subseq=None):
    """Returns the Hamming weights of all non-negative integers in the
    interval [0, 2**n).

    The terms of this sequence are generated via a recurrence relation.
    If 'subseq' is passed all the terms in [0, 2**(n-1)), the rest of
    of the terms can be generated more efficiently.
    """
    weights = subseq if subseq else [0]
    while len(weights) < 2**n:
        for i in weights.copy():
            weights.append(i + 1)
    return weights