FastPrepAll Root-to-Leaf Paths in a Binary Tree
Problems/All Root-to-Leaf Paths in a Binary Tree

All Root-to-Leaf Paths in a Binary Tree

Google logoGoogle● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for all root-to-leaf paths as strings. It wants DFS with backtracking and no global state, plus an iterative follow-up for very deep trees. It does not give the separator, the output order, what to return for an empty tree, the input limits, or a function signature. We added those. Our version is about 95% the same as the reported question.

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"]
root
1253

The left leaf produces 1->2->5, followed by the right leaf path 1->3.

Example 2

root = [1]return = ["1"]
root
1

The root is also a leaf, so the only path contains its value.

Example 3

root = []return = []
root
empty

An empty tree has no root-to-leaf paths.

Example 4

root = [-1,-2,3,null,-4]return = ["-1->-2->-4","-1->3"]
root
-1-2-43

Negative values are preserved in the path strings, and the left path still appears before the right path.

Constraints

  • The tree contains between 0 and 100000 nodes.
  • -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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
      // Write your code here.
    }
}
Checking account…