257. Binary Tree Paths
Binary Tree Paths (LeetCode 257) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Preorder Traversal subpattern.
How to solve Binary Tree Paths
Binary Tree Paths is a Preorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master preorder traversal for tree construction, inversion, and path problems. Visit root before children.
Work towards the solution in these steps:
- Hint 1: Think about how you can traverse the tree to visit every root-to-leaf path.
- Hint 2: You can use recursion (DFS) to traverse from the root to each leaf, building the path as you go.
- Hint 3: When you reach a leaf node (no left or right child), add the current path to your result list.
The reference solution runs in O(n * h) time and O(h) space.
The Binary Tree Paths solution and editorial walks through the full approach with reference code.
Problem statement
Given the root of a binary tree, return all root-to-leaf paths in any order.
A leaf is a node with no children.
Example 1:
Input: root = [1,2,3,null,5] Output: ["1->2->5","1->3"]
Example 2:
Input: root = [1] Output: ["1"]
Constraints:
- The number of nodes in the tree is in the range [1, 100].
- -100 <= Node.val <= 100
Topics and companies
Topics: String, Backtracking, Tree, Depth-First Search, Binary Tree.
Reported in interviews at Facebook, Google, Apple, Bloomberg.
How to practise Binary Tree Paths on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n * h) time and O(h) space.
Open Binary Tree Paths in the code editor, or read the Binary Tree Paths editorial for a worked solution with its approach and complexity analysis.
Where Binary Tree Paths sits in the DSA pattern sheet
- Tree Traversal Patterns (DFS & BFS) pattern guide
- Recursive Preorder Traversal subpattern
- The full DSA patterns sheet
Problems related to Binary Tree Paths
Other problems that use the same Recursive Preorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:
- Binary Tree Preorder Traversal — Easy, same subpattern
- Construct Binary Tree from Preorder and Inorder Traversal — Medium, same subpattern
- Construct Quad Tree — Medium, same subpattern
- Count Good Nodes in Binary Tree — Medium, same subpattern
- Flatten Binary Tree to Linked List — Medium, same subpattern
- Insert into a Binary Search Tree — Medium, same subpattern
Binary Tree Paths FAQ
What is the LeetCode number of Binary Tree Paths?
Binary Tree Paths is LeetCode problem 257, rated Easy.
What pattern is used to solve Binary Tree Paths?
Binary Tree Paths is a Preorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Preorder Traversal subpattern.
What is the time complexity of Binary Tree Paths?
The reference solution runs in O(n * h) time and O(h) space.
Which companies ask Binary Tree Paths?
Binary Tree Paths has been reported in interviews at Facebook, Google, Apple, Bloomberg.