class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
path = []
self.dfs(res, path, n, k, start=1)
return res
def dfs(self, res, path, n, k, start):
if len(path) == k:
res.append(path[:]) # [:]和下面的copy保留一个即可, python中一定要有
return
for i in range(start, n+1): # 本层集合中的元素,递归N叉树,树节点孩子数量就是集合的大小
path.append(i)
self.dfs(res, path.copy(), n, k, i+1)
path.pop()