Created
February 5, 2019 01:55
-
-
Save ttsugriy/d55bfffdeef86989c2568a0c4cd131ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def smallestFromLeaf(self, root: TreeNode) -> str: | |
| smallest_so_far: 'List[Tuple[int]]' = [(27,)] # sentinel larger than any lowercase letter | |
| def explore(node: TreeNode, stack: 'List[int]') -> None: | |
| stack.append(node.val) | |
| if not node.left and not node.right: # leaf | |
| smallest_so_far[0] = min(smallest_so_far[0], tuple(reversed(stack))) | |
| else: | |
| for child in (node.left, node.right): | |
| if child: | |
| explore(child, stack) | |
| stack.pop() | |
| explore(root, []) | |
| return "".join(chr(val + ord('a')) for val in smallest_so_far[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment