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:
Consider the following binary tree:
1
/ \
2 3
\
5
Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]
Example 2:
Consider a single-node tree:
1
Input: root = [1]
Output: ["1"]
Write a function that takes the root of a binary tree and returns a list of strings, where each string represents a path from the root to a leaf node. What is the time and space complexity of your solution? Consider edge cases such as an empty tree or a tree with only one node.
The most straightforward approach is to use Depth-First Search (DFS) to traverse the binary tree. We explore each possible path from the root to a leaf node. As we traverse, we build the path string. When we reach a leaf node, we add the path to our result.
dfs(node, current_path)
:
current_path
string.current_path
to the list of paths.dfs
on the left child, updating current_path
.dfs
on the right child, updating current_path
.dfs
function starting from the root with an empty current_path
.class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def binaryTreePaths(root):
if not root:
return []
paths = []
def dfs(node, path):
if not node.left and not node.right:
paths.append(path + str(node.val))
return
if node.left:
dfs(node.left, path + str(node.val) + "->")
if node.right:
dfs(node.right, path + str(node.val) + "->")
dfs(root, "")
return paths
While the previous solution is conceptually simple, the repeated string concatenation in each recursive call can introduce overhead. An optimized approach involves using a list to store the path and only converting it to a string when a leaf node is reached.
dfs(node, current_path)
:
current_path
list.current_path
list to a string using "->" as a separator and add it to the list of paths.dfs
on the left child, updating current_path
.dfs
on the right child, updating current_path
.current_path
to backtrack correctly.dfs
function starting from the root with an empty current_path
.class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def binaryTreePaths(root):
if not root:
return []
paths = []
def dfs(node, path):
if not node:
return
path.append(str(node.val))
if not node.left and not node.right:
paths.append("->".join(path))
else:
if node.left:
dfs(node.left, path)
if node.right:
dfs(node.right, path)
path.pop()
dfs(root, [])
return paths
current_path
list. In the worst case (skewed tree), H can be N, resulting in O(N) space.