Python Straight Forward Solution


  • 3

    v is path sum, nc is the number of leaves

    def pathSum(self, nums):
            s = {}
            l = {}
            for i in nums[::-1]:
                a, b, c = i / 100, i / 10 % 10, i % 10
                l[a, b] = max(1, l.get((a + 1, b * 2 - 1), 0) + l.get((a + 1, b * 2), 0))
                s[a, b] = s.get((a + 1, b * 2 - 1), 0) + s.get((a + 1, b * 2), 0) + l[a, b] * c
            return s.get((1, 1), 0)

Log in to reply
 

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