Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Second Minimum Node In a Binary Tree

Updated
2 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "671" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds. Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree. If no such second minimum value exists, output -1 instead. Example 1: Input: root = [2,2,5,null,null,5,7] Output: 5 Explanation: The smallest value is 2, the second smallest value is 5. Example 2: Input: root = [2,2,2] Output: -1 Explanation: The smallest value is 2, but there isn't any second smallest value. Constraints: The number of nodes in the tree is in the range [1, 25]. 1 <= Node.val <= 231 - 1 root.val == min(root.left.val, root.right.val) for each internal node of the tree.

Explanation

Here's a solution to find the second minimum value in the special binary tree, focusing on efficiency and optimality.

  • High-Level Approach:

    • Identify the minimum value in the tree, which is the root's value due to the given property.
    • Perform a Depth-First Search (DFS) to find the smallest value that is greater than the minimum value.
    • If no such value exists, return -1.
  • Complexity:

    • Runtime Complexity: O(N), where N is the number of nodes in the tree (in the worst case, we may need to visit all nodes).
    • Storage Complexity: O(H), where H is the height of the tree, due to the recursion stack in DFS. In the worst case (skewed tree), H = N. In the best case (balanced tree), H = logN.

Code

    class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def findSecondMinimumValue(root: TreeNode) -> int:
    if not root:
        return -1

    min_val = root.val
    second_min = float('inf')

    def dfs(node):
        nonlocal second_min

        if not node:
            return

        if node.val > min_val and node.val < second_min:
            second_min = node.val
        elif node.val == min_val:  # Only explore children if they could potentially lead to a second minimum
            dfs(node.left)
            dfs(node.right)

    dfs(root)

    return second_min if second_min != float('inf') else -1

More from this blog

C

Chatmagic blog

2894 posts