Short Simple Python - O(N) Time - O(1) Space


  • 4
    A
    def getMinimumDifference(self, root):
        self.previous = self.minimum = float('inf')
        
        def inorder(node):
            if node:
                inorder(node.left)
                self.minimum = min(self.minimum, abs(node.val-self.previous))
                self.previous = node.val
                inorder(node.right)
        
        inorder(root)
        return self.minimum
    
    

  • 0
    L

    Recursion is never O(1) space


Log in to reply
 

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