题目描述

题目解释
这个题类似于之前做的某一道题,其实算法还是要追踪到树的深度遍历,相当于便利叶子节点的路径记录。不过递归的过程就相当于件数根据树进行遍历了。
代码如下
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        def dfs(nums,size,depth,path,used,res):
            if depth==size:
                res.append(path[:])
                return
            for i in range(size):
                if not used[i]:
                    used[i]=True
                    path.append(nums[i])
                    dfs(nums,size,depth+1,path,used,res)
                    used[i]=False
                    path.pop()
        size=len(nums)
        if len(nums)==0:
            return []
        used=[False for _ in range(size)]
        res=[]
        dfs(nums,size,0,[],used,res)
        return res
                










![Problem #8 [Easy]](https://img-blog.csdnimg.cn/direct/8e3470409369490b923d625980bc87d9.png)






