# 10 examples of 'iterative dfs' in Python

Every line of 'iterative dfs' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``26def dfs(self, root, ls, res):27    if not root.left and not root.right:28        res.append(ls + str(root.val))29    if root.left:30        self.dfs(root.left, ls + str(root.val) + "-&gt;", res)31    if root.right:32        self.dfs(root.right, ls + str(root.val) + "-&gt;", res)``
``14def DFS(path, visited, res):15    if len(path) == N:16        res += [cal_V(path)]17        return18    for i in xrange(N):19        if not visited[i]:20            if i&gt;0 and not visited[i-1] and Y[i] == Y[i-1]:21                continue22            visited[i] = True23            DFS(path + [Y[i]], visited, res)24            visited[i] = False``
``26def dfs(self, node, level, res):27    if node:28        if level &gt; len(res): res.insert(0,[])29        res[-level].append(node.val)30        self.dfs(node.left, level+1, res)31        self.dfs(node.right,level+1, res)32        return res``
``11def DFSrecur(self,s,visited):12	visited[s] = True13	print(s , end=" ")14	for i in self.graph[s]:15		if visited[i] == False:16			self.DFSrecur(i,visited)``
``77def dfs(x):78    if x.is_leaf:79        yield x80    else:81        for x in order.val:82            if x == 'l':83                yield from dfs(x.lson)84            elif x == 'n':85                yield x86            elif x == 'r':87                yield from dfs(x.rson)``
``1def dfs(self):2    """3    Computes the initial source vertices for each connected component4    and the parents for each vertex as determined through depth-first-search5    :return: initial source vertices for each connected component, parents for each vertex6    :rtype: set, dict7    """8    parents = {}9    components = set()10    to_visit = []1112    for vertex in self.get_vertex():13        if vertex not in parents:14            components.add(vertex)15        else:16            continue1718        to_visit.append(vertex)1920        while to_visit:21            v = to_visit.pop()2223            for neighbor in self.get_neighbor(v):24                if neighbor not in parents:25                    parents[neighbor] = v26                    to_visit.append(neighbor)2728    return components, parents``
``71def dfs(node, k):72    if node in visited or not node or k &lt; 0: return73    if k == 0:74        ans.append(node.val)75        return76    dfs(node.left, k-1)77    dfs(node.right, k-1)``
``15def __dfs(self, candidates, size, start, path, residue, res):16    """17    :param candidates: 无重复元素的数组18    :param size: 数组的长度，避免一直使用 len(candidates)19    :param start: 从 candidates 索引的第几位开始考虑20    :param path: 深度优先搜索沿途经过的路径21    :param residue: 剩余值22    :param res: 存放结果集的列表23    :return:24    """25    if residue == 0:26        res.append(path[:])27        return2829    for index in range(start, size):30        # 因为我们已经将数组预处理成升序数组，一旦发现当前遍历的数比剩余值还大，循环就可以停止了31        if residue - candidates[index] &lt; 0:32            break33        path.append(candidates[index])34        # 注意：因为数字可以无限制重复被选取，因此起始位置还是自己35        self.__dfs(candidates, size, index, path, residue - candidates[index], res)36        path.pop()``
``33def DFS(self, v):3435    # Mark all the vertices as not visited36    visited = [False] * (len(self.graph))3738    # Call the recursive helper function to print39    # DFS traversal40    self.DFSUtil(v, visited)``
``10def DFSUtil(self,v,visited):11    visited[v]=True12    print v,1314    for i in self.graph[v]:15        if visited[i] == False:16            self.DFSUtil(i, visited)``