Given the root of a binary tree, return all root-to-leaf paths in any order.

A leaf is a node with no children.

Input & Output

Example 1 — Basic Binary Tree
$ Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]
💡 Note: Tree has two root-to-leaf paths: 1→2→5 (left subtree) and 1→3 (right subtree)
Example 2 — Single Node Tree
$ Input: root = [1]
Output: ["1"]
💡 Note: Tree with only root node - the single path is just the root value
Example 3 — Complete Binary Tree
$ Input: root = [1,2,3,4,5]
Output: ["1->2->4","1->2->5","1->3"]
💡 Note: Three root-to-leaf paths: left-left (1→2→4), left-right (1→2→5), and right (1→3)

Constraints

  • The number of nodes in the tree is in the range [1, 100]
  • -100 ≤ Node.val ≤ 100

Visualization

Tap to expand
Binary Tree Paths: Find All Root-to-Leaf Paths1235Path 1: 1→2→5Path 2: 1→3Output:["1->2->5", "1->3"]🎯 Key Insight: Use DFS to traverse all paths and collect strings at leaf nodes
Understanding the Visualization
1
Input Tree
Binary tree with nodes connected by parent-child relationships
2
DFS Traversal
Explore all paths from root to leaf nodes
3
Collect Paths
Return all complete root-to-leaf paths as strings
Key Takeaway
🎯 Key Insight: Use DFS traversal to systematically explore all root-to-leaf paths and build path strings
Asked in
Google 45 Facebook 38 Amazon 32 Apple 28
285.0K Views
Medium Frequency
~15 min Avg. Time
4.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen