<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>&nbsp;is <em>flip equivalent</em> 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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="Flipped Trees Diagram" src="https://assets.leetcode.com/uploads/2018/11/29/tree_ex.png" style="width: 500px; height: 220px;" />
<pre>
<strong>Input:</strong> 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]
<strong>Output:</strong> true
<strong>Explanation: </strong>We flipped at nodes with values 1, 3, and 5.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root1 = [], root2 = []
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root1 = [], root2 = [1]
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

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

class Solution {
public:
    bool flipEquiv(TreeNode* root1, TreeNode* root2) {
        TreeNode *curr1;
        TreeNode *curr2;
        queue<TreeNode *> bfsQ1, bfsQ2;
        pair<int, int> pair1, pair2;
        
        if (root1 == NULL && root2 == NULL) {
            return true;
        }
        
        if (root1 == NULL || root2 == NULL) {
            return false;
        }
        
        if (root1->val != root2->val) {
            return false;
        }
        
        bfsQ1.push(root1);
        bfsQ2.push(root2);
        
        while (!bfsQ1.empty() && !bfsQ2.empty()) {
            curr1 = bfsQ1.front();
            curr2 = bfsQ2.front();
            
            bfsQ1.pop();
            bfsQ2.pop();
            
            if (curr1->left == NULL) {
                pair1.first = -1;
            } else {
                pair1.first = curr1->left->val;
            }
            if (curr1->right == NULL) {
                pair1.second = -1;
            } else {
                pair1.second = curr1->right->val;
            }
            
            if (curr2->left == NULL) {
                pair2.first = -1;
            } else {
                pair2.first = curr2->left->val;
            }
            if (curr2->right == NULL) {
                pair2.second = -1;
            } else {
                pair2.second = curr2->right->val;
            }
            
            if (pair1.first == pair2.first && pair1.second == pair2.second) {
                if (pair1.first != -1 && pair2.first != -1) {
                    bfsQ1.push(curr1->left);
                    bfsQ2.push(curr2->left);
                }
                if (pair1.second != -1 && pair2.second != -1) {
                    bfsQ1.push(curr1->right);
                    bfsQ2.push(curr2->right);
                }
            } else if (pair1.first == pair2.second && pair1.second == pair2.first) {
                if (pair1.first != -1 && pair2.second != -1) {
                    bfsQ1.push(curr1->left);
                    bfsQ2.push(curr2->right);
                }
                if (pair1.second != -1 && pair2.first != -1) {
                    bfsQ1.push(curr1->right);
                    bfsQ2.push(curr2->left);
                }
            } else {
                return false;
            }
        }
        
        return (bfsQ1.empty() && bfsQ2.empty());
    }
};
14900