Python, in-order with yield


  • 0
    J
    def getMinimumDifference(self, root):
        min_diff = float('inf')
        nodes = self.inorder(root)
        prev = nodes.next()
        for n in nodes:
            min_diff = min(min_diff, n.val - prev.val)
            prev = n
        return min_diff
        
    def inorder(self, root):
        if root:
            for n in self.inorder(root.left): yield n
            yield root
            for n in self.inorder(root.right): yield n
    

Log in to reply
 

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