Python fast iterative


  • 0
    T
    class Solution(object):
        def getMinimumDifference(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            stack = []
            current = root
            last = None
            minVal = float('inf')
            while stack or current:
                if current: 
                    stack.append(current)
                    current = current.left
                else:
                    node = stack.pop()
                    current = node.right
                    if not last is None:
                        minVal = min(minVal, node.val - last)
                    last = node.val
            return minVal
    

Log in to reply
 

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