Depth-First Search (DFS)

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(log⁡n)O(\log n) space in the best case,… Continue reading Depth-First Search (DFS)