🧑‍💻 PROOF || 💯 BEATS || ✨ EXPLAIN WITH EXAMPLE || 🍵 JAVA
\n\n\n\n\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n##### My first thought is that two trees are flip equivalent if:\n\n- A
```\n```\n\n![image.png](https://assets.leetcode.com/users/images/59a9cece-88cb-4dd7-a1c0-8fb37a1c352e_1729762786.0730948.png)\n\n```\n```\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n##### **My first thought is that two trees are flip equivalent if**:\n\n- At each node, either their left and right children are directly equal, or their left child can be swapped with the right child to make them equal. We can use a recursive approach to explore all possible configurations of flipped and non-flipped subtrees at each node.\n\n\n```\n```\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **If both $$nodes$$ ($$node1$$ and $$node2$$) are null, they are trivially equivalent, so return $$true$$.**\n\n2. **If only one $$node$$ is null or the values of $$node1$$ and $$node2$$ don\'t match, return $$false$$.**\n\n3. **Recursively check if**:\n    - Either both left children and both right children are flip equivalent (no flipping case).\n    - Or, the left child of one tree is flip equivalent to the right child of the other tree and vice versa (flipped case).\n\n4. **Return $$true$$ if either case holds.**\n```\n```\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n**$$O(n)$$, where $$n$$ is the number of nodes in the trees. We visit each node once during the recursive process.**\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n**$$O(h)$$, where $$h$$ is the height of the tree due to the recursive call stack. In the worst case (a skewed tree), this can be $$O(n)$$, but for a balanced tree, it would be $$O(logn)$$.**\n```\n```\n# Code\n```java []\nclass Solution {\n    public boolean flipEquiv(TreeNode root1, TreeNode root2) {\n        return helper(root1, root2);\n    }\n\n    private boolean helper(TreeNode node1, TreeNode node2) {\n        if (node1 == null && node2 == null) \n            return true;\n        \n        if (node1 == null || node2 == null || node1.val != node2.val)\n            return false;\n        \n        return (helper(node1.left, node2.left) || helper(node1.left, node2.right)) &&\n                (helper(node1.right, node2.left) || helper(node1.right, node2.right));\n    }\n}\n```\n\n```\n```\n\n![upvote.png](https://assets.leetcode.com/users/images/d3499ad9-c942-4b73-b5ae-33f1fdcaed6e_1729762833.6315725.png)\n
['Tree', 'Depth-First Search', 'Recursion', 'Binary Tree', 'Java']