InferenceAI

Enter at least two characters to search.

Start free

Root-to-Leaf Paths

easyTrees15 min10 XP

Return every root-to-leaf path as a string like "1->2->5", in left-to-right order.

Trees are given in level order as a list, with None for a missing child (for example [3, 9, 20, None, None, 15, 7]).

Examples

EXAMPLE WALKTHROUGHtwo paths

A guided view of the published example, not a trace of your code.

INPUT[[1,2,3,null,5]]
TASKroot_to_leaf_paths()
EXPECTED OUTPUT["1->2->5","1->3"]

Start with the input values shown in this example.

Example 1 of 3

Input: values = [1,2,3,null,5]

Output: ["1->2->5","1->3"]

Explanation: two paths

Input: values = [1]

Output: ["1"]

Explanation: single node

Input: values = []

Output: []

Explanation: empty

Hint 1

Depth-first search, passing the path so far.

Topics

DSATreesPython