Python Solution Using Stack


  • 0
    M
        def getMinimumDifference(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            stack, visited = [root], [root.val]
            while stack:
                node = stack.pop()
                if node.left:
                    visited.append(node.left.val)
                    stack.append(node.left)
                if node.right:
                    visited.append(node.right.val)
                    stack.append(node.right)
            min_dist = float('Inf')
            visited.sort()
            print visited
            for i in xrange(len(visited)-1):
                dist = abs(visited[i]-visited[i+1])
                min_dist = min(min_dist, dist)
            return min_dist
    

Log in to reply
 

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