Python - Simple and easy-understand solution (DFS)

  • 0
    class Solution(object):
        def pathSum(self, root, sum):
            :type root: TreeNode
            :type sum: int
            :rtype: int
            self.sum = sum
            self.result = 0
            if not root:
                return 0
            self.dfs(root, [])
            return self.result
        def dfs(self, node, vl):
            if not node:
            vl = [i+node.val for i in vl] + [node.val]
            self.result += vl.count(self.sum)
            self.dfs(node.left, vl)
            self.dfs(node.right, vl)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.