Python, no external variable outside of helper function

  • 0

    Most of the solutions here use some sort of variable outside of a helper function to keep track of either the tilt or the sum. Here is a solution where that is not needed. It runs a little slower than other solutions, though.

    def findTilt(self, root):
        def tiltSum(node):
            if not node: return 0,0
            L, R = tiltSum(node.left), tiltSum(node.right)
            return abs(L[1]-R[1])+L[0]+R[0], node.val+L[1]+R[1]
        return tiltSum(root)[0]

Log in to reply

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