Second Minimum Node In a Binary Tree (LeetCode 671): Solution and Explanation

Easy
Tree
Depth First Search
Binary Tree

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

  1. Set first = root.val and ans = -1.
  2. DFS from the root. A null node returns right away.
  3. If node.val > first, set ans = node.val when ans is -1 or bigger, then return without visiting the children.
  4. Otherwise the node equals first, so recurse into both children.
  5. Return ans.

Step-by-step example

root = [2,2,5,null,null,5,7]
  1. first = 2 (the root). ans = -1.
  2. Visit the root: it equals 2, so search both children.
  3. Left child 2: equals first and is a leaf. Nothing below it.
  4. Right child 5: above 2, so ans = 5. Its children 5 and 7 are at least 5, so they are skipped.
  5. 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.ans
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
DFS with early stop O(n)
Track two minimums O(n)
Collect all values O(n log n)
Time

O(n)

Each node is visited at most once, and skipped subtrees make it faster in practice.

Space

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.