Skip to main content
LeetCode 951, Medium. Topics: Tree, Depth-First Search, Binary Tree. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 13 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026