Date: 2026-06-06
Time: 15:25
binary-tree-paths/solution.pyThis file solves LeetCode 257 — Binary Tree Paths. It finds every root-to-leaf path in a binary tree and returns them as arrow-delimited strings like "1->2->5". The file is self-contained: it defines the tree node class, the solution function, and a full test suite.
TreeNode — Standard binary tree node with val, left, right. Defined locally rather than imported, making the file standalone. The type annotation for children uses the union syntax ("TreeNode | None") as a forward-reference string.
binarytreepaths(root) — The public API. Takes an optional root node, returns a list of path strings. The empty-tree case returns [] immediately.
dfs(node, path) — Inner closure that performs the actual traversal. It mutates the outer results list as a side effect rather than returning values. The path parameter is a string that accumulates the arrow-delimited representation as recursion deepens.
Closure-based DFS with string accumulation. The inner dfs function captures results from the enclosing scope. Path state is carried via the path string parameter — since strings are immutable in Python, each recursive call gets its own copy, so there's no need for explicit backtracking. This is a common alternative to passing a mutable list and joining at leaf nodes.
Root value pre-seeded. The initial call dfs(root, str(root.val)) seeds the path with the root's value. Subsequent recursive calls append "->" + str(child.val) before descending. This means the path string is always built one level ahead — the caller adds the child's value, not the child itself. This avoids a trailing arrow or an extra conditional inside dfs.
Imports: typing.List, typing.Optional, unittest — all stdlib. No external dependencies.
Imported by: The "Imported By" list in the prompt is misleading — those are test files for *other* problems, not actual importers of this module. This file defines TreeNode locally, so other solutions that need a tree node likely define their own or share a common one. The cross-references likely come from a static analysis tool picking up the TreeNode class name rather than real import edges.
1. binarytreepaths(None) → returns [].
2. binarytreepaths(root) → initializes empty results, calls dfs(root, str(root.val)).
3. dfs checks if node is a leaf (no left, no right). If so, appends the accumulated path to results and returns.
4. If node.left exists, recurse with path + "->" + str(node.left.val).
5. If node.right exists, recurse with path + "->" + str(node.right.val).
6. After DFS completes, results contains all root-to-leaf paths.
left and right are None. Internal nodes with one child are not leaves — the single-child branch is followed."->", no leading or trailing separator. Negative values are handled naturally by str().None. The function trusts its input — there's no cycle detection, no type checking on node values. A None root is the only guarded case. If root.val is not stringifiable, str() would raise, but that's outside the problem's contract.