Python DFS-don't need global variable

  • 0
    def longestConsecutive(self, root):
        :type root: TreeNode
        :rtype: int
        def helper(parent, root, length):
            if not root:
                return length
            if root.val-parent.val == 1:
                length += 1
                length = 1
            return max(length, helper(root, root.left, length), helper(root, root.right, length))
        if not root:
            return 0
        return max( helper(root, root.left, 1), helper(root, root.right, 1))

Log in to reply

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