Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Binary Tree Paths (easy)
Table of Contents

Problem Statement

Examples

Try it yourself

Problem Statement

Given the root node of a binary tree, return all unique paths from the root node to the leaf node in any order.

Examples

Example 1:

  • Input: root = [1,2,3,null,null,4,5]
Image
  • Expected Output: ["1->2", "1->3->4", "1->3->5"]
  • Justification: The binary tree has one root-to-leaf path for the left child (1->2) and two for the right child, branching into 4 and 5 respectively.

Example 2:

  • Input: root = [5,3,6,2,4,null,null,1]
Image
  • Expected Output: ["5->3->2->1", "5->3->4", "5->6"]
  • Justification: There are three paths leading to leaf nodes: one through the left subtree down to 1, another through the left subtree to 4, and one path through the right subtree to 6.

Example 3:

  • Input: root = [2,null,3,null,4,null,5]
Image
  • Expected Output: ["2->3->4->5"]
  • Justification: There's only one path from the root to the leaf, cascading down the right children.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .
Mark as Completed

Table of Contents

Problem Statement

Examples

Try it yourself