671. Second Minimum Node In a Binary Tree
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the second smallest value in a special binary tree. This tree has a specific property: for any node with two children, its value is the minimum of its children's values. This implies that node.val <= node.left.val and node.val <= node.right.val. A direct consequence of this property is that the root of the tree, root, holds the minimum value in the entire tree. Let's call this value min1.
Solution
Second Minimum Node In a Binary Tree in Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findSecondMinimumValue(self, root: Optional[TreeNode]) -> int:
if not root:
return -1
# The root's value is the absolute minimum in the tree.
self.min1 = root.val
# Initialize second minimum as -1 (not found).
self.secondMin = -1
# Start DFS from the root.
self.dfs(root)
return self.secondMin
def dfs(self, node: Optional[TreeNode]):
# Base case: if the node is null, return.
if not node:
return
# If the node's value is greater than the minimum value,
# it's a candidate for the second minimum.
if node.val > self.min1:
# If this is the first candidate found, or if it's smaller than
# the current second minimum, update secondMin.
if self.secondMin == -1 or node.val < self.secondMin:
self.secondMin = node.val
# Optimization: No need to traverse deeper, as children will be >= node.val.
return
# If node.val is equal to min1, we need to explore its children.
self.dfs(node.left)
self.dfs(node.right)
Second Minimum Node In a Binary Tree in Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
long secondMin = -1;
int min1;
public int findSecondMinimumValue(TreeNode root) {
if (root == null) {
return -1;
}
// The root's value is the absolute minimum in the tree.
min1 = root.val;
// Start DFS from the root.
dfs(root);
// Return the found second minimum value, cast back to int.
return (int) secondMin;
}
private void dfs(TreeNode node) {
// Base case: if the node is null, return.
if (node == null) {
return;
}
// If the node's value is greater than the minimum value,
// it's a candidate for the second minimum.
if (node.val > min1) {
// If this is the first candidate found, or if it's smaller than
// the current second minimum, update secondMin.
if (secondMin == -1 || node.val < secondMin) {
secondMin = node.val;
}
// Optimization: No need to traverse deeper, as children will be >= node.val.
return;
}
// If node.val is equal to min1, we need to explore its children.
dfs(node.left);
dfs(node.right);
}
}
Second Minimum Node In a Binary Tree in C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int min1;
long long secondMin = -1;
void dfs(TreeNode* node) {
// Base case: if the node is null, return.
if (!node) {
return;
}
// If the node's value is greater than the minimum value,
// it's a candidate for the second minimum.
if (node->val > min1) {
// If this is the first candidate we've found, or if it's smaller than
// the current second minimum, update secondMin.
if (secondMin == -1 || (long long)node->val < secondMin) {
secondMin = node->val;
}
// Optimization: No need to traverse deeper, as children will be >= node->val.
return;
}
// If node->val is equal to min1, we need to explore its children.
dfs(node->left);
dfs(node->right);
}
public:
int findSecondMinimumValue(TreeNode* root) {
if (!root) {
return -1;
}
// The root's value is the absolute minimum in the tree.
min1 = root->val;
// Start DFS from the root.
dfs(root);
// Return the found second minimum value.
return secondMin;
}
};
Second Minimum Node In a Binary Tree in TypeScript
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function findSecondMinimumValue(root: TreeNode | null): number {
if (!root) {
return -1;
}
// The root's value is the absolute minimum in the tree.
const min1 = root.val;
// Initialize second minimum as -1 (not found).
// TypeScript's number type can handle values up to 2^53-1, so it's safe.
let secondMin = -1;
function dfs(node: TreeNode | null) {
// Base case: if the node is null, return.
if (!node) {
return;
}
// If the node's value is greater than the minimum value,
// it's a candidate for the second minimum.
if (node.val > min1) {
// If this is the first candidate found, or if it's smaller than
// the current second minimum, update secondMin.
if (secondMin === -1 || node.val < secondMin) {
secondMin = node.val;
}
// Optimization: No need to traverse deeper, as children will be >= node.val.
return;
}
// If node.val is equal to min1, we need to explore its children.
dfs(node.left);
dfs(node.right);
}
// Start DFS from the root.
dfs(root);
return secondMin;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.