Binary Tree Paths

Given the root of a binary tree, return all root-to-leaf paths in the tree as strings in the form a->b->c. Pattern focus: Preorder DFS (Root First). Build the path string while moving downward and record it when a leaf is reached.

Input Format

root = binary tree root

Output Format

all root-to-leaf paths as strings

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^4 <= node values <= 10^4
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [1,2,3,null,5]

Output:

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

Explanation:

The tree has two root-to-leaf paths.

Example 2:

Input:

root = [1]

Output:

["1"]

Explanation:

A single node is also a root-to-leaf path.

Loading...
Binary Tree Paths - Binary Tree DSA Problem