Second Minimum Node In a Binary Tree (LeetCode 671): Solution and Explanation
In a special tree where every parent equals the smaller of its two children, return the second smallest distinct value, or -1 if every node holds the same value.
Intuition
Each parent is the minimum of its children, so values can only stay the same or grow as you go down. That makes the root the smallest value in the whole tree.
Now think about a node whose value is bigger than the root. Everything below it is at least as big as that node, so the node itself is the best candidate its branch can offer.
So the search only needs to walk through nodes equal to the root value. On each branch it stops at the first bigger node and records it. The smallest recorded value is the answer.
Approach: DFS with early stop
- Set
first = root.valandans = -1. - DFS from the root. A null node returns right away.
- If
node.val > first, setans = node.valwhenansis -1 or bigger, then return without visiting the children. - Otherwise the node equals
first, so recurse into both children. - Return
ans.
Step-by-step example
root = [2,2,5,null,null,5,7]
- first = 2 (the root). ans = -1.
- Visit the root: it equals 2, so search both children.
- Left child 2: equals first and is a leaf. Nothing below it.
- Right child 5: above 2, so ans = 5. Its children 5 and 7 are at least 5, so they are skipped.
- The search is over. Return 5.
Code: Python, Java, C++
1class Solution:
2 def findSecondMinimumValue(self, root: Optional[TreeNode]) -> int:
3 self.first = root.val
4 self.ans = -1
5 def dfs(node):
6 if not node:
7 return
8 if node.val > self.first:
9 if self.ans == -1 or node.val < self.ans:
10 self.ans = node.val
11 return
12 dfs(node.left)
13 dfs(node.right)
14 dfs(root)
15 return self.ans1class Solution {
2 private int first;
3 private int ans;
4 public int findSecondMinimumValue(TreeNode root) {
5 first = root.val;
6 ans = -1;
7 dfs(root);
8 return ans;
9 }
10 private void dfs(TreeNode node) {
11 if (node == null) return;
12 if (node.val > first) {
13 if (ans == -1 || node.val < ans) {
14 ans = node.val;
15 }
16 return;
17 }
18 dfs(node.left);
19 dfs(node.right);
20 }
21}1class Solution {
2public:
3 int findSecondMinimumValue(TreeNode* root) {
4 first = root->val;
5 ans = -1;
6 dfs(root);
7 return ans;
8 }
9private:
10 int first = 0;
11 int ans = -1;
12 void dfs(TreeNode* node) {
13 if (!node) return;
14 if (node->val > first) {
15 if (ans == -1 || node->val < ans) {
16 ans = node->val;
17 }
18 return;
19 }
20 dfs(node->left);
21 dfs(node->right);
22 }
23};Time and space complexity
O(n)
Each node is visited at most once, and skipped subtrees make it faster in practice.
O(h)
The recursion stack holds one frame per level of the tree.
Other approaches
Collect all values (time O(n log n), space O(n))
Traverse the whole tree into a set, sort it and return the second element if it exists. Simple, but it ignores the special structure.
Track two minimums (time O(n), space O(h))
Traverse every node and keep the smallest value above the root. Linear, but it never prunes a subtree.
Common mistakes
- Using a sentinel like Integer.MAX_VALUE for 'not found'. Node values can be 2^31 - 1, so a real answer would look like no answer. Use -1 or a separate flag.
- Returning the smaller child of the root. The second minimum can sit deep in the subtree that holds the root value.
- Counting duplicates as distinct. A tree of all 2s has no second minimum, so the answer is -1.
Frequently asked questions
Why can the search stop at a node that is bigger than the root?
Every node below it is at least its value, because parents are the minimum of their children. Nothing deeper can beat it.
Is the root always the minimum?
Yes. Each parent equals its smaller child, so by induction every node is at least the root value.
When is the answer -1?
When every node holds the same value, including a tree with a single node.