-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmallest_from_leaf.py
55 lines (44 loc) · 1.69 KB
/
smallest_from_leaf.py
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
53
54
55
from typing import Optional
import unittest
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def smallestFromLeaf(self, root: Optional[TreeNode]) -> str:
"""
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".
A leaf of a node is a node that has no children.
"""
def dfs(node, path, result):
if not node:
return
path.append(chr(node.val + ord('a')))
if not node.left and not node.right:
result.append(''.join(path[::-1]))
dfs(node.left, path, result)
dfs(node.right, path, result)
path.pop()
result = []
dfs(root, [], result)
return min(result) if result else ''
# Time complexity: O(n)
# Space complexity: O(n)
import unittest
class TestSmallestFromLeaf(unittest.TestCase):
def test_example_1(self):
root = TreeNode(0)
root.left = TreeNode(1)
root.right = TreeNode(2)
root.left.left = TreeNode(3)
root.left.right = TreeNode(4)
root.right.left = TreeNode(3)
root.right.right = TreeNode(4)
self.assertEqual(Solution().smallestFromLeaf(root), 'dba')
if __name__ == '__main__':
unittest.main()