Back to problems Sign in to unlock
Leetcode
Easy
Trees
Recursion
Strings
Binary Tree Paths
Return every root-to-leaf path in a binary tree as a string using -> between node values.
Acceptance 100%
Problem Statement
Given the root of a binary tree, return all root-to-leaf paths in any order.
A root-to-leaf path is a path that starts at the root node and ends at a leaf node. Represent each path as a string by joining node values with ->.
This problem is mainly about traversing the tree while building the current path and recording it when a leaf is reached.
Input Format
- The input is the root of a binary tree.
- Each node contains an integer value.
- The tree is provided in the usual binary-tree structure rather than as a single flat array.
Output Format
- Return a list of strings.
- Each string should represent one root-to-leaf path in the form
root->...->leaf. - The order of paths does not matter.
Constraints
- The tree has at least 1 node.
- Node values may be positive, negative, or zero.
- The tree can be skewed or balanced.
- Use the path formatting exactly with
->between values.
Examples
Sample cases returned by the problem API.
Example 1
Input
root = [1,2,3,null,5]
Output
["1->2->5","1->3"]
Explanation
The root-to-leaf paths are 1->2->5 and 1->3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.