Problem Statement
This is LeetCode problem 671: "Second Minimum Node In a Binary Tree," commonly appearing in LinkedIn coding interviews with tags Tree, DFS, and Binary Tree. You are given a non-empty special binary tree where each node has either exactly two children or zero children (no nodes with one child), all node values are non-negative integers, and for any node with children, its value equals the minimum of its two children's values: root.val = min(root.left.val, root.right.val). The task is to find and return the second smallest distinct value among all nodes in the tree; if no such second minimum exists (all nodes have the same value), return -1.[1][3][7]
Key Properties
Input/Output Examples
| Example | Tree Structure | Input Representation | Output | Explanation |
|---------|----------------|----------------------|--------|-------------|
| 1 | <br> 2<br> / \<br> 2 5<br> / \<br> 5 7<br> | root = [2][2][5][5][7] | 5 | Values are {2,5,7}; smallest is 2, second smallest is 5. [1][5] |
| 2 | <br> 2<br> / \<br> 2 2<br> / \<br>2 2<br> | root = [2,2,2,null,null,2,2] | -1 | All values are 2; no second minimum. [3] |
Constraints