You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.As a reminder, any shorter prefix of a string is lexicographically smaller.
For example, "ab" is lexicographically smaller than "aba".
from leetcode_py import TreeNodeclass Solution: # Time: O(n^2) worst case for string building on a skewed tree # Space: O(n) def smallest_from_leaf(self, root: TreeNode[int] | None) -> str: best = "" def dfs(node: TreeNode[int] | None, path: str) -> None: nonlocal best if node is None: return path = chr(ord("a") + node.val) + path if node.left is None and node.right is None: if not best or path < best: best = path return dfs(node.left, path) dfs(node.right, path) dfs(root, "") return best