Advertisement

Second Minimum Node In a Binary Tree - LeetCode 671 Solution

Second Minimum Node In a Binary Tree - Complete Solution Guide

Second Minimum Node In a Binary Tree is LeetCode problem 671, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 e

Detailed Explanation

The problem asks us to find the second smallest value in a special binary tree. This tree has a unique property: each node's value is the minimum of its two children's values (if it has children). The root node's value is therefore the smallest value in the entire tree. We need to return the second smallest unique value. If there's no second distinct minimum value (i.e., all nodes have the same value), we should return -1. The values in the tree are non-negative.

Solution Approach

The provided Python code uses a Depth-First Search (DFS) approach to traverse the tree. It initializes `second_min` to infinity and `min_val` to the root's value. The DFS function recursively explores the tree. If a node's value is greater than `min_val`, it updates `second_min` if the node's value is smaller than the current `second_min`. After traversing the entire tree, if `second_min` is still infinity, it means there's no second minimum value, and we return -1; otherwise, we return `second_min`.

Step-by-Step Algorithm

  1. Step 1: Initialize `second_min` to infinity and `min_val` to the root's value.
  2. Step 2: Define a recursive DFS function that takes a node as input.
  3. Step 3: In the DFS function, if the node is null, return.
  4. Step 4: If the node's value is greater than `min_val`, compare it with `second_min` and update `second_min` if the node's value is smaller.
  5. Step 5: Recursively call the DFS function on the left and right children of the node.
  6. Step 6: After the DFS traversal is complete, check if `second_min` is still infinity. If it is, return -1; otherwise, return `second_min`.

Key Insights

  • Insight 1: The root node's value is the smallest value in the entire tree due to the tree's property. This value serves as our base for finding the second minimum.
  • Insight 2: We can use Depth-First Search (DFS) or Breadth-First Search (BFS) to traverse the tree and compare each node's value with the smallest value.
  • Insight 3: The second minimum value must be strictly greater than the smallest value. If a node's value is greater than the smallest value and smaller than our current candidate for second minimum, we update the second minimum.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

This problem involves: Tree, Depth-First Search, Binary Tree.

Companies

Asked at: LinkedIn.