🚀 100% Faster Solution | Clean & Simple Code in Java, C++, Python & JS 🔥
\uD83C\uDF33 *Problem Explanation:\n##### You are given two binary trees. The task is to determine whether the trees are flip equivalent, meaning they are the s
# \uD83C\uDF33 ****Problem Explanation****:\n##### You are given two binary trees. The task is to determine whether the trees are flip equivalent, meaning they are the same when we flip some of their subtrees. A subtree flip happens when we swap the left and right children of a node. Two trees are flip equivalent if they are structurally the same after a series of such flips.\n\n---\n\n# ***Java Solution \uD83C\uDDEE\uD83C\uDDF3***\n```\nclass Solution {\n    public boolean flipEquiv(TreeNode root1, TreeNode root2) {\n        // \uD83D\uDE0E Base case: both nodes are null, trees are flip equivalent at this point.\n        if (root1 == null && root2 == null) return true;\n        \n        // \u274C If one node is null and the other isn\'t, they\'re not flip equivalent.\n        if (root1 == null || root2 == null) return false;\n        \n        // \u274C If node values don\'t match, they can\'t be flip equivalent.\n        if (root1.val != root2.val) return false;\n        \n        // \uD83C\uDF1F Check two possibilities:\n        // 1\uFE0F\u20E3 No flip needed: left with left and right with right.\n        // 2\uFE0F\u20E3 Flip needed: left with right and right with left.\n        return (flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)) || \n               (flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left));\n    }\n}\n\n```\n\n---\n\n# ***C++ Solution \uD83C\uDDEB\uD83C\uDDF7***\n```\nclass Solution {\npublic:\n    bool flipEquiv(TreeNode* root1, TreeNode* root2) {\n        // \uD83D\uDE0E Base case: both nodes are null, trees are flip equivalent at this point.\n        if (root1 == nullptr && root2 == nullptr) return true;\n        \n        // \u274C If one node is null and the other isn\'t, they\'re not flip equivalent.\n        if (root1 == nullptr || root2 == nullptr) return false;\n        \n        // \u274C If node values don\'t match, they can\'t be flip equivalent.\n        if (root1->val != root2->val) return false;\n        \n        // \uD83C\uDF1F Check two possibilities:\n        // 1\uFE0F\u20E3 No flip needed: left with left and right with right.\n        // 2\uFE0F\u20E3 Flip needed: left with right and right with left.\n        return (flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right)) || \n               (flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left));\n    }\n};\n\n```\n\n---\n\n# ***Python Solution \uD83D\uDC0D***\n```\nclass Solution:\n    def flipEquiv(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:\n        # \uD83D\uDE0E Base case: both nodes are None, trees are flip equivalent at this point.\n        if not root1 and not root2:\n            return True\n        \n        # \u274C If one node is None and the other isn\'t, they\'re not flip equivalent.\n        if not root1 or not root2:\n            return False\n        \n        # \u274C If node values don\'t match, they can\'t be flip equivalent.\n        if root1.val != root2.val:\n            return False\n        \n        # \uD83C\uDF1F Check two possibilities:\n        # 1\uFE0F\u20E3 No flip needed: left with left and right with right.\n        # 2\uFE0F\u20E3 Flip needed: left with right and right with left.\n        return (self.flipEquiv(root1.left, root2.left) and self.flipEquiv(root1.right, root2.right)) or \\\n               (self.flipEquiv(root1.left, root2.right) and self.flipEquiv(root1.right, root2.left))\n\n```\n\n---\n\n# ***JavaScript Solution \uD83C\uDDEF\uD83C\uDDF5***\n```\nvar flipEquiv = function(root1, root2) {\n    // \uD83D\uDE0E Base case: both nodes are null, trees are flip equivalent at this point.\n    if (root1 === null && root2 === null) return true;\n    \n    // \u274C If one node is null and the other isn\'t, they\'re not flip equivalent.\n    if (root1 === null || root2 === null) return false;\n    \n    // \u274C If node values don\'t match, they can\'t be flip equivalent.\n    if (root1.val !== root2.val) return false;\n    \n    // \uD83C\uDF1F Check two possibilities:\n    // 1\uFE0F\u20E3 No flip needed: left with left and right with right.\n    // 2\uFE0F\u20E3 Flip needed: left with right and right with left.\n    return (flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)) || \n           (flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left));\n};\n\n```\n\n---\n\n# ****Explanation \uD83C\uDF93****\n##### * \uD83D\uDE0E ***Base Case***: Both nodes being `null` means we have reached the end of that subtree, so they are equivalent.\n##### * \u274C ***Unequal Nodes***: If one node is `null` and the other isn\'t, or if their values don\u2019t match, they are not equivalent.\n##### * \uD83C\uDF1F ***Recursive Check***: We check both possibilities: (1) if the left child matches the left child and the right matches the right, or (2) if we swap left and right children and then they match.
['Tree', 'Recursion', 'C', 'Python', 'Java', 'JavaScript']