851. Find All Root-to-Leaf Paths

EasyTreesBinary TreeDFSBacktracking

Given the root of a binary tree, return all root-to-leaf paths, each as the list of node values from the root down to a leaf. Return the paths as a nested array, in left-to-right (preorder) order. The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Nested array — each root-to-leaf path.

Examples

Example 1
Input: [1,2,3,null,5]
Output: [[1,2,5],[1,3]]
Explanation: Two root-to-leaf paths.
Example 2
Input: [1]
Output: [[1]]
Explanation: Single-node path.
Example 3
Input: []
Output: []
Explanation: No paths.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →