Python 32ms solution

  • 0

    key note: son nodes are not smaller than father node, so, if the root value is bigger than exist sec_min, stop searching and return.

    def findSecondMinimumValue(self, root, sec_min=-1):
            :type root: TreeNode
            :rtype: int
            if (not root.left) or (not root.right) or (0< sec_min <= root.val):
                return sec_min
            if root.left.val > root.right.val:
                sec_min = min(root.left.val, sec_min) if sec_min != -1 else root.left.val
                return self.findSecondMinimumValue(root.right, sec_min)
            elif root.left.val < root.right.val:
                sec_min = min(root.right.val, sec_min) if sec_min != -1 else root.right.val                
                return self.findSecondMinimumValue(root.left, sec_min)
                return min(self.findSecondMinimumValue(root.left, sec_min), self.findSecondMinimumValue(root.right, sec_min))

Log in to reply

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