leetcode -. 988 minimum string starting from the leaf nodes

Not her own writing. .

class Solution:
    def smallestFromLeaf(self, root: TreeNode) -> str:
        self.ans="~"
        def dfs(node,A):
            if node:
                A.append(chr(node.val+ord('a')))
                if not node.left and not node.right:
                    self.ans=min(self.ans,''.join(reversed(A)))
                dfs(node.left,A)
                dfs(node.right,A)
                A.pop()
        dfs(root,[])
        return self.ans
When execution: 64 ms, beat the 78.13% of users in all python3 submission
Memory consumption: 15.4 MB, beat the 7.41% of users in all python3 submission
 
——2019.11.22

Guess you like

Origin www.cnblogs.com/taoyuxin/p/11914658.html