logo
Problems

Binary Tree Paths

Problem

Given a binary tree, return all root-to-leaf paths.

Example

Given the following binary tree:

   1
 /   \
2     3
 \
  5

All root-to-leaf paths are:

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

Solution

DFS and keep track of the strings (e.g. "1->2" at node 2).

Online Judge