DFS explores every subtree until it hits a dead end before switching to another path. It can be done recursively, or it can be done iteratively. Let’s reuse the TreeNode definition from our BFS post. 1. Recursive Solution The recursive approach achieves O(n)O(n) time in the worst case, O(logn)O(\log n) space in the best case,… Continue reading Depth-First Search (DFS)
Tag: stack
Stack & Queue in Go
1. Stack This is your good ol’ LIFO (last-in, first-out) structure. Just imagine a Pringles tube. A basic stack should have the following methods: In Go, you can use a slice as the underlying data structure. Notice how initiating the store slice in the NewStack[T any]() constructor is optional? This is because we have enough… Continue reading Stack & Queue in Go