Skip to main content

Second Minimum Node In a Binary Tree

LeetCode 671 | Difficulty: Easy​

Easy

Problem Description​

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 <= 2^31 - 1`

- `root.val == min(root.left.val, root.right.val)` for each internal node of the tree.

Topics: Tree, Depth-First Search, Binary Tree


Approach​

Tree DFS​

Traverse the tree recursively (or with a stack). At each node, decide: what information do I need from the left/right subtrees? Process: go left β†’ go right β†’ combine results. Consider preorder, inorder, or postorder traversal based on when you need to process the node.

When to use

Path problems, subtree properties, tree structure manipulation.


Solutions​

Solution 1: C# (Best: 84 ms)​

MetricValue
Runtime84 ms
Memory25 MB
Date2021-09-23
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int FindSecondMinimumValue(TreeNode root) {
if(root == null || root.left == null) return -1;
int result = helper(root, root.val);
if(result == Int32.MaxValue)
{
if(root.left != null && root.val != Int32.MaxValue && (root.left.val == Int32.MaxValue || root.right.val == Int32.MaxValue))
{
return Int32.MaxValue;
}
return -1;
}
return result;
}

public int helper(TreeNode root, int min)
{
if(root == null)
{
return Int32.MaxValue;
}

if(root.val > min)
{
return root.val;
}
int left = helper(root.left, min);
int right = helper(root.right, min);

return Math.Min(left, right);
}
}
πŸ“œ 1 more C# submission(s)

Submission (2021-09-23) β€” 96 ms, 24.2 MB​

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int FindSecondMinimumValue(TreeNode root) {
if (root == null || root.left == null) return -1;
Queue<TreeNode> q = new Queue<TreeNode>();
bool found = false;
q.Enqueue(root);
int first = root.val, second = Int32.MaxValue;
while(q.Count != 0)
{
var front = q.Dequeue();
if(front.val > first && front.val <= second)
{
second = front.val;
found = true;
continue;
}
if(front.left == null) continue;
q.Enqueue(front.left);
q.Enqueue(front.right);
}
return found ? second : -1;
}
}

Complexity Analysis​

ApproachTimeSpace
Tree Traversal$O(n)$$O(h)$

Interview Tips​

Key Points
  • Start by clarifying edge cases: empty input, single element, all duplicates.
  • Consider: "What information do I need from each subtree?" β€” this defines your recursive return value.