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

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    class stack_entry {
    public:
        TreeNode*   n1;
        TreeNode*   n2;
        int         which;
        bool        sw;
    };

public:
    bool flipEquiv(TreeNode* root1, TreeNode* root2) {
        stack_entry* cur;
        stack_entry* node_stack;
        TreeNode* node1;
        TreeNode* node2;

        if (root1 == nullptr)
            return root2 == nullptr;
        if (root2 == nullptr)
            return false;

        if (root1->val != root2->val)
            return false;

        node_stack = new stack_entry [100];

        node_stack[0].n1 = root1;
        node_stack[0].n2 = root2;
        node_stack[0].which = 0;
        node_stack[0].sw = false;
        cur = node_stack;

        for (;;) {
            bool sw;

            node1 = cur->n1;
            node2 = cur->n2;
            if (cur->which == 0) {
                node1 = node1->left;
                node2 = node2->left;
                sw = node1 == nullptr
                     ? node2 != nullptr
                     : node2 == nullptr || node1->val != node2->val;
                if (sw)
                    node2 = cur->n2->right;
            } else if (cur->which == 1) {
                node1 = node1->right;
                node2 = cur->sw ? node2->left : node2->right;
            } else {
                if (cur == node_stack)
                    break;
                --cur;
                continue;
            }
            ++cur->which;
            cur->sw = sw;
            if (node1 == nullptr) {
                if (node2 != nullptr)
                    return false;
                continue;
            }
            if (node2 == nullptr || node1->val != node2->val)
                return false;
            ++cur;
            cur->n1 = node1;
            cur->n2 = node2;
            cur->which = 0;
            cur->sw = false;
        }

        return true;
    }
};
14800