Python solution (beat 100%)

  • 0

    First of all, this question is not valid, as the assumption is father node value is the smaller value of its children. However, in the tests, there are cases when all the children have the same value of parent.
    The following solution, nevertheless can address this problem also.

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    class Solution:
        def findSecondMinimumValue(self, root):
            :type root: TreeNode
            :rtype: int
            def traversal(node):
                if node == None or node.left == None:
                    return None
                first, second = None, None
                if node.left.val < node.right.val:
                    first = traversal(node.left)
                    second = node.right
                elif node.left.val > node.right.val:
                    first = traversal(node.right)
                    second = node.left
                    first = traversal(node.left)
                    second = traversal(node.right)
                if first and second: 
                    if first.val < second.val: return first
                    return second
                if not first: return second
                return first
            found = traversal(root)
            if not found: return -1
            return found.val

Log in to reply

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