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

# Flip Equivalent Binary Trees Python Solution

> Tested Python solution for LeetCode 951 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 951, [Medium](/catalog/medium). 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/flip-equivalent-binary-trees/description/).

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

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

## Problem

\<p>For a binary tree \<strong>T\</strong>, we can define a \<strong>flip operation\</strong> as follows: choose any node, and swap the left and right child subtrees.\</p>

\<p>A binary tree \<strong>X\</strong> is \<strong>flip equivalent\</strong> to a binary tree \<strong>Y\</strong> if and only if we can make \<strong>X\</strong> equal to \<strong>Y\</strong> after some number of flip operations.\</p>

\<p>Given the roots of two binary trees \<code>root1\</code> and \<code>root2\</code>, return \<code>true\</code> if the two trees are flip equivalent or \<code>false\</code> otherwise.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2018/11/29/tree_ex.png)

```
Input: root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7]
Output: true
Explanation: We flipped at nodes with values 1, 3, and 5.
```

```
Input: root1 = [], root2 = []
Output: true
```

```
Input: root1 = [], root2 = [1]
Output: false
```

### Constraints

\<ul>
\<li>The number of nodes in each tree is in the range \<code>\[0, 100]\</code>.\</li>
\<li>Each tree will have \<strong>unique node values\</strong> in the range \<code>\[0, 99]\</code>.\</li>
\</ul>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flip_equivalent_binary_trees/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flip_equivalent_binary_trees/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) where h is the tree height
    def flip_equiv(self, root1: TreeNode[int] | None, root2: TreeNode[int] | None) -> bool:
        if root1 is None or root2 is None:
            return root1 is root2
        if root1.val != root2.val:
            return False
        no_flip = self.flip_equiv(root1.left, root2.left) and self.flip_equiv(
            root1.right, root2.right
        )
        flip = self.flip_equiv(root1.left, root2.right) and self.flip_equiv(root1.right, root2.left)
        return no_flip or flip
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(h) where h is the tree height |

## Tags

[NeetCode All](/catalog/neetcode).


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