Solution Review: Print All the Paths

Let’s go through the detailed solution review of the challenge given in the previous lesson.

Solution

For this problem, we’ll use a stack and follow the depth-first approach. Whenever we traverse a node, we’ll add that node to the stack. When we reach a leaf, we’ll print the whole list (from the root node to that leaf node). When we return from the function, we’ll remove the element that was added to the stack when we entered this function.

Solution code

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy