112. 路径总和



class Solution(object):
    def hasPathSum(self, root, sumt):
        """
        :type root: TreeNode
        :type sumt: int
        :rtype: bool
        """
        if not root and sum:
            return False
        stack = [(root, [root.val])]
        while stack:
            node, temp = stack.pop()
            if not node.left and not node.right and sum(temp) == sumt:
                return True
            if node.left:
                stack.append((node.left, temp + [node.left.val]))
            if node.right:
                stack.append((node.right, temp + [node.right.val]))
        return False

posted @ 2020-08-30 16:48  人间烟火地三鲜  阅读(120)  评论(0编辑  收藏  举报