Binary Tree Paths - Problem
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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code