Return all root-to-leaf tree paths

Quick Overview

This question evaluates competence with binary tree structures, path enumeration and traversal techniques along with the ability to represent traversal results as formatted strings.

Return all root-to-leaf tree paths

Company: Capital One

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given the root of a binary tree, return all **root-to-leaf** paths. - A **leaf** is a node with no children. - A path should be represented as a string with node values separated by `"->"`. ## Input - `root`: the root node of a binary tree (each node has `val`, `left`, `right`). ## Output - A list of strings, each string representing one root-to-leaf path. ## Example For the tree: ``` 1 / \ 2 3 \ 5 ``` Output: - `["1->2->5", "1->3"]` ## Constraints - Number of nodes up to 10^4. - Node values are integers.

Quick Answer: This question evaluates competence with binary tree structures, path enumeration and traversal techniques along with the ability to represent traversal results as formatted strings.

|Home/Coding & Algorithms/Capital One
Capital One logo
Capital One
Dec 15, 2025, 12:00 AM
mediumMachine Learning EngineerOnsiteCoding & Algorithms
11
0

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

  • A leaf is a node with no children.
  • A path should be represented as a string with node values separated by "->" .

Input

  • root : the root node of a binary tree (each node has val , left , right ).

Output

  • A list of strings, each string representing one root-to-leaf path.

Example

For the tree:

   1
  / \
 2   3
  \
   5

Output:

  • ["1->2->5", "1->3"]

Constraints

  • Number of nodes up to 10^4.
  • Node values are integers.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...