-
Notifications
You must be signed in to change notification settings - Fork 3
/
951.cpp
53 lines (50 loc) · 1.94 KB
/
951.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* 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 {
public:
bool dfs(TreeNode* node1, TreeNode* node2) {
if (node1 == nullptr && node2 == nullptr) return true;
if (node1 == nullptr || node2 == nullptr || node1->val != node2->val) return false;
return (dfs(node1->left, node2->left) && dfs(node1->right, node2->right)) || (dfs(node1->left, node2->right) && dfs(node1->right, node2->left));
}
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
return dfs(root1, root2);
}
};
// v2
// /**
// * 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 {
// public:
// bool dfs(TreeNode* node1, TreeNode* node2) {
// if (node1 == nullptr && node2 == nullptr) return true;
// if (node1 == nullptr || node2 == nullptr) return false;
// if (node1->val != node2->val) return false;
// bool originalLeft = dfs(node1->left, node2->left);
// bool originalRight = dfs(node1->right, node2->right);
// bool exchangeLeft = dfs(node1->left, node2->right);
// bool exchangeRight = dfs(node1->right, node2->left);
// return ((originalLeft && originalRight) || (exchangeLeft && exchangeRight));
// }
// bool flipEquiv(TreeNode* root1, TreeNode* root2) {
// return dfs(root1, root2);
// }
// };