https://leetcode.com/problems/smallest-string-starting-from-leaf/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import heapq
class Solution(object):
def smallestFromLeaf(self, root):
"""
:type root: TreeNode
:rtype: str
"""
alphabet_list = list(string.ascii_lowercase)
h = []
def check_list(path):
stringman = ""
for i in range(len(path)-1, -1, -1):
#print(i,"i")
stringman += alphabet_list[path[i]]
heapq.heappush(h, (stringman, stringman))
def printview(root, path):
if root:
if not root.left and not root.right:
#print(root.val, path + [root.val], depth)
check_list(path+[root.val])
printview(root.left, path +[root.val])
printview(root.right, path + [root.val])
path_list = []
depth = 0
printview(root, path_list)
print(h)
return heapq.heappop(h)[1]
'알고리즘 문제풀기' 카테고리의 다른 글
1048. Longest String Chain (0) | 2019.12.05 |
---|---|
1219. Path with Maximum Gold (0) | 2019.12.04 |
792. Number of Matching Subsequences (0) | 2019.11.27 |
392. Is Subsequence (0) | 2019.11.27 |
946. Validate Stack Sequences (0) | 2019.11.26 |