Many recursive solutions to problems of size N follow the pattern:
Step 1: Solve the problem for the smallest input (say, n = 1)
Step 2: Given the solution to the same problem of size n = k-1 (k <= N), solve it for n = k.
We can see the inductive nature of this, which is why we typically use induction to prove recursive algorithms. For some problems, like recursive Fibonacci, this pattern emerges in an obvious way. My question is if, say, Binary Tree Traversal can be seen as also following this pattern?
Take Depth First Search for instance:
def DFS(root):
if root == None:
return
print(root.value)
DFS(root.left)
DFS(root.right)
I understand the code and the call stack, but I'm struggling to articulate exactly what the steps 1 and 2 are for DFS (in holding with the structure of recursive solutions outlined above). It can't be that the only base case is when the current node is None
, because traversing a single node should also be a base case.
If I say that line 4, the one containing the print
statement, is a base case it wouldn't make much sense because it runs for every sub-tree.