From b38403f6de2ddf4f41c8568854167cf093fa2658 Mon Sep 17 00:00:00 2001
From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com>
Date: Tue, 1 Aug 2023 20:43:48 -0700
Subject: [PATCH] Update README.md
---
trees/README.md | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/trees/README.md b/trees/README.md
index a62cc09..107811e 100644
--- a/trees/README.md
+++ b/trees/README.md
@@ -50,7 +50,7 @@ def bfs(root):
- similar to BFS, deep-first search (DFS) can also be used to find the path from the root node to the target node.
-- prefered if you want to visit every node.
+- it's a good option for finding the first path (instead of the first path), or if you want to visit every node.
- overall, we only trace back and try another path after we reach the deepest node. as a result, the first path you find in DFS is not always the shortest path.
- we first push the root node to the stack, then we try the first neighbor and push its node to the stack, etc.
- when we reach the deepest node, we need to trace back. when we track back, we pop the deepest node from the stack, which is actually the last node pushed to the stack.
@@ -59,7 +59,20 @@ def bfs(root):
- if the depth of the tree is too large, stack overflow might happen, therefore iterative solutions might be better.
- work with stacks.
+
+```python
+def dfs(root, visited):
+ if root is None:
+ return root
+ while root.next:
+ if root.next not in visited:
+ visited.add(root.next)
+ return dfs(root.next, visited)
+ return False
+```
+
+
#### in-order