Easy python solution with O(n) complexity.


  • 0
    J
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def findSecondMinimumValue(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """      
            if root.left == None and root.right == None:
                return -1
            
            left_res, right_res = root.left.val, root.right.val
            
            if root.left.val == root.val:
                left_res = self.findSecondMinimumValue(root.left)
            if root.right.val == root.val:
                right_res = self.findSecondMinimumValue(root.right)
                
            if left_res == -1 and right_res > -1:
                return right_res
            elif left_res > -1 and right_res == -1:
                return left_res
            
            return min(left_res, right_res) ```

Log in to reply
 

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