The problem. Given the root of a binary tree, return all root-to-leaf paths, each written as a string like "1->2->5". A leaf is a node with no children.
Input: root = [1, 2, 3, null, 5]
Output: ["1->2->5", "1->3"]
Input: root = [1]
Output: ["1"]There is exactly one path per leaf, so the job is: walk down, remember the way you came, and write it out each time you reach a leaf. The approaches differ in how they remember the way.
Members only · free account
The rest of this lesson, with 6 more sections, 2 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson