Flip Equivalent Binary Trees LeetCode solution in C++

1
class Solution {
2
public:
3
// Isommorphic tree
4
bool flipEquiv(TreeNode *root1, TreeNode *root2) {
5
if (!root1 and !root2) return true;
6
if (!root1 or !root2) return false;
7
if (root1->val != root2->val) return false;
8

9
// if flipped then left subtree == right subtree and right subtree == left
10
// subtree
11
bool a = flipEquiv(root1->left, root2->right);
12
bool b = flipEquiv(root1->right, root2->left);
13

14
// if not flipped then left subtree == left subtree and right subtree ==
15
// right subtree
16
bool aa = flipEquiv(root1->left, root2->left);
17
bool bb = flipEquiv(root1->right, root2->right);
18

19
return (a and b) || (aa and bb);
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0