Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created February 5, 2019 01:55
Show Gist options
  • Select an option

  • Save ttsugriy/d55bfffdeef86989c2568a0c4cd131ef to your computer and use it in GitHub Desktop.

Select an option

Save ttsugriy/d55bfffdeef86989c2568a0c4cd131ef to your computer and use it in GitHub Desktop.
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