Python Inorder Traversal 3 Line


  • 4
    class Solution(object):
        def getMinimumDifference(self, root):
            nums = self.inorder(root)
            return min(nums[i+1]-nums[i] for i in range(len(nums)-1))
            
        def inorder(self, root):
            return self.inorder(root.left) + [root.val] + self.inorder(root.right) if root else []

  • 0
    A

    Space complexity is O(n). Can reduce to O(log(n)).


Log in to reply
 

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