Python simple iterative DFS using stack

  • 0

    Using stack make it easier to convert recursive DFS to iterative DFS.
    Hope it will be helpful for ppl who are more familiar with recursive way, and want to convert to iterative way directly.

        def subsets(self, nums):
            res = []
            stack = [([],0)]
            while stack:
                st, start = stack.pop()
                for i in range(start, len(nums)):
                    stack.append((st + [nums[i]], i + 1))
            return res

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.