Carry out usual DFS
and keep track of current string that is getting created. Once you reach leaves
, update the smallest
current string. We are using an external variable for this which is why we have to use the nonlocal
variable.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity here is since we would visit all nodes.
-
Memory
The memory usage is since we use the implicit call stack for recursion.
— A