Python solution with O(1) space complexity


  • 0
    M

    'res' always have 2 items at most.

    class Solution:
        
        def bfs(self, root, res):
            if root:
                if len(res) == 1:
                    if res[0] < root.val:
                        res.append(root.val)
                else:
                    if res[1] > root.val and res[0] != root.val:
                        del res[1]
                        res.append(root.val)
                self.bfs(root.left, res)
                self.bfs(root.right, res)
        
        def findSecondMinimumValue(self, root):
            res = [root.val]
            self.bfs(root, res)
            return res[1] if len(res) > 1 else -1
    
    

Log in to reply
 

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