Question 8 - Python function to traverse a binary tree path


The following data science interview question is an example of a more classic programming/algorithm question. You may find something similar in a software engineering interview. Here the question will be testing for your ability traverse a binary tree using python.

Question: For a given binary tree, return all root-to-leaf paths.

We'll use the tree below to walk through how your code should work. "A" is the root, and "E" would be consider the leaf in the path A-> B -> E. The expectation for your function is to go return all root to leaf combinations.

The output of your code given the tree below would be: ["A -> B -> E", "A -> B -> D", "A-> C"]

A

/ \

B C

/ \

E D



Ace your next data science interview

Get better at data science interviews by solving a few questions per week