LeetCode 671 Second Minimum Node In a Binary Tree

LeetCode 671 Second Minimum Node In a Binary Tree Problem

Download Code
# Definition for a binary tree node.
# class TreeNode(object):
#     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
    #     """
    #     # Brute force
    #     values = set()
    #     self.dfs(root, values)
    #     ans, min_value = float('inf'), root.val
    #     for n in values:
    #         if min_value < n and n < ans:
    #             ans = n
    #     return ans if ans < float('inf') else -1

    # def dfs(self, root, values):
    #     if not root:
    #         return
    #     values.add(root.val)
    #     self.dfs(root.left, values)
    #     self.dfs(root.right, values)

    def findSecondMinimumValue(self, root):
        if not root:
            return -1
        ans = float('inf')
        min_val = root.val
        stack = [root]
        while stack:
            curr = stack.pop()
            if not curr:
                continue
            if min_val < curr.val < ans:
                ans = curr.val
            elif curr.val == min_val:
                stack.append(curr.left)
                stack.append(curr.right)
        return ans if ans < float('inf') else -1
Download Second Minimum Node In a Binary Tree.py

List of all Second Minimum Node In a Binary Tree problems

Leetcode 671 Second Minimum Node In a Binary Tree problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

self.val = x

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.