> ## 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.

# Cousins in Binary Tree Python Solution

> Tested Python solution for LeetCode 993 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 993, [Easy](/catalog/easy). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/cousins-in-binary-tree/description/).

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

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

## Problem

Given the `root` of a binary tree with unique values and the values of two different nodes of the tree `x` and `y`, return `true` if the nodes corresponding to the values `x` and `y` in the tree are cousins, or `false` otherwise.

Two nodes of a binary tree are **cousins** if they have the same depth with different parents.

Note that in a binary tree, the root node is at the depth `0`, and children of each depth `k` node are at the depth `k + 1`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2019/02/12/q1248-01.png)

```
Input: root = [1,2,3,4], x = 4, y = 3
Output: false
```

![Example 2](https://assets.leetcode.com/uploads/2019/02/12/q1248-02.png)

```
Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
Output: true
```

![Example 3](https://assets.leetcode.com/uploads/2019/02/13/q1248-03.png)

```
Input: root = [1,2,3,null,4], x = 2, y = 3
Output: false
```

### Constraints

* The number of nodes in the tree is in the range \[2, 100]
* 1 \<= Node.val \<= 100
* Each node has a unique value
* x != y
* x and y exist in the tree

## Solution

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

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


class Solution:
    # Time: O(n) - single traversal of the tree
    # Space: O(h) - recursion stack, h is the tree height
    def is_cousins(self, root: TreeNode[int] | None, x: int, y: int) -> bool:
        info: dict[int, tuple[int, int | None]] = {}

        def dfs(node: TreeNode[int] | None, parent: int | None, depth: int) -> None:
            if node is None:
                return
            info[node.val] = (depth, parent)
            dfs(node.left, node.val, depth + 1)
            dfs(node.right, node.val, depth + 1)

        if root is None:
            return False
        dfs(root, None, 0)
        dx, px = info[x]
        dy, py = info[y]
        return dx == dy and px != py
```

## Complexity

| Time | Space |
| - | - |
| O(n) - single traversal of the tree | O(h) - recursion stack, h is the tree height |

## Tags


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