forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_paths.py
More file actions
52 lines (39 loc) · 1.3 KB
/
binary_tree_paths.py
File metadata and controls
52 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Binary Tree Paths
Given a binary tree, return all root-to-leaf paths as a list of strings
in the format "root->...->leaf".
Reference: https://en.wikipedia.org/wiki/Binary_tree#Combinatorics
Complexity:
Time: O(n)
Space: O(n)
"""
from __future__ import annotations
from algorithms.tree.tree import TreeNode
def binary_tree_paths(root: TreeNode | None) -> list[str]:
"""Return all root-to-leaf paths in the binary tree.
Args:
root: The root of the binary tree.
Returns:
A list of strings representing each root-to-leaf path.
Examples:
>>> binary_tree_paths(None)
[]
"""
result: list[str] = []
if root is None:
return result
_dfs(result, root, str(root.val))
return result
def _dfs(result: list[str], root: TreeNode, current: str) -> None:
"""Perform depth-first search to collect all root-to-leaf paths.
Args:
result: The list accumulating path strings.
root: The current node being visited.
current: The path string built so far.
"""
if root.left is None and root.right is None:
result.append(current)
if root.left:
_dfs(result, root.left, current + "->" + str(root.left.val))
if root.right:
_dfs(result, root.right, current + "->" + str(root.right.val))