> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Second Minimum Node In a Binary Tree

> Tested Python solution for LeetCode 671 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 671, [Easy](/catalog/easy). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 671   # by problem number
lcpy gen -s second_minimum_node_in_a_binary_tree   # by problem name
```

## Problem

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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/10/15/smbt1.jpg)

```
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](https://assets.leetcode.com/uploads/2020/10/15/smbt2.jpg)

```
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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/second_minimum_node_in_a_binary_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/second_minimum_node_in_a_binary_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(h)
    def find_second_minimum_value(self, root: TreeNode[int] | None) -> int:
        if root is None:
            return -1
        return self._dfs(root, root.val)

    def _dfs(self, node: TreeNode[int] | None, smallest: int) -> int:
        if node is None:
            return -1
        if node.val > smallest:
            return node.val
        left = self._dfs(node.left, smallest)
        right = self._dfs(node.right, smallest)
        if left == -1:
            return right
        if right == -1:
            return left
        return min(left, right)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(h) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.