Skip to content

Commit b656113

Browse files
Create flip_equivalent.cpp
1 parent 29d086e commit b656113

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

flip_equivalent.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
15+
if (!root1 && !root2) return true;
16+
if (!root1 && root2 || root1 && !root2 || root1->val != root2->val)
17+
return false;
18+
19+
return flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right)|| flipEquiv(root1->right, root2->left) && flipEquiv(root1->left, root2->right);
20+
}
21+
};

0 commit comments

Comments
 (0)