All Root-to-Leaf Paths in a Binary Tree
Updated
Problem statement
Given the root of a binary tree, return every path from the root to a leaf.
- Write each path as a string by joining its node values with
->. - Return paths in left-to-right depth-first order: completely visit a node's left subtree before its right subtree.
- Return an empty array for an empty tree.
A leaf has no left child and no right child. Do not use global or static mutable state.
Deep-tree follow-up
How can an explicit stack preserve the same traversal and backtracking behavior without risking recursive call-stack overflow?
Function
binaryTreePaths(root: TreeNode) → List<String>Examples
Example 1
root = [1,2,3,null,5]return = ["1->2->5","1->3"]The left leaf produces 1->2->5, followed by the right leaf path 1->3.
Example 2
root = [1]return = ["1"]The root is also a leaf, so the only path contains its value.
Example 3
root = []return = []An empty tree has no root-to-leaf paths.
Example 4
root = [-1,-2,3,null,-4]return = ["-1->-2->-4","-1->3"]Negative values are preserved in the path strings, and the left path still appears before the right path.
Constraints
- The tree contains between
0and100000nodes. -2^31 <= Node.val <= 2^31 - 1.- The tree may be skewed to a depth of
100000.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Binary Tree Preorder TraversalAnduril · ONSITE INTERVIEWAnduril interview questions
- Root-to-Node Path in a Binary TreeAmazon · ONSITE INTERVIEW
- Preorder, Inorder and Postorder of a Binary TreeCapital One · OA · ONSITE INTERVIEWCapital One interview questions
- All Balanced Strings from Minimum Parenthesis InsertionsRubrik · ONSITE INTERVIEWRubrik interview questions
- Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Root-to-Leaf Paths with a Target SumBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
- Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEWNavan interview questions