forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flip-equivalent-binary-trees.cpp
92 lines (87 loc) · 2.71 KB
/
flip-equivalent-binary-trees.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// bfs solution
class Solution {
public:
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
queue<TreeNode *> q1({root1}), q2({root2});
while (!q1.empty() && !q2.empty()) {
auto node1 = q1.front(); q1.pop();
auto node2 = q2.front(); q2.pop();
if (!node1 && !node2) {
continue;
}
if (!node1 || !node2 || node1->val != node2->val) {
return false;
}
if ((!node1->left && !node2->right) ||
(node1->left && node2->right && node1->left->val == node2->right->val)) {
q1.emplace(node1->right);
q1.emplace(node1->left);
} else {
q1.emplace(node1->left);
q1.emplace(node1->right);
}
q2.emplace(node2->left);
q2.emplace(node2->right);
}
return q1.empty() && q2.empty();
}
};
// Time: O(n)
// Space: O(h)
// iterative dfs solution
class Solution2 {
public:
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
vector<TreeNode *> stk1 = {root1}, stk2 = {root2};
while (!stk1.empty() && !stk2.empty()) {
auto node1 = stk1.back(); stk1.pop_back();
auto node2 = stk2.back(); stk2.pop_back();
if (!node1 && !node2) {
continue;
}
if (!node1 || !node2 || node1->val != node2->val) {
return false;
}
if ((!node1->left && !node2->right) ||
(node1->left && node2->right && node1->left->val == node2->right->val)) {
stk1.emplace_back(node1->right);
stk1.emplace_back(node1->left);
} else {
stk1.emplace_back(node1->left);
stk1.emplace_back(node1->right);
}
stk2.emplace_back(node2->left);
stk2.emplace_back(node2->right);
}
return stk1.empty() && stk2.empty();
}
};
// Time: O(n)
// Space: O(h)
// recursive dfs solution
class Solution3 {
public:
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
if (!root1 && !root2) {
return true;
}
if (!root1 || !root2 || root1->val != root2->val) {
return false;
}
return (flipEquiv(root1->left, root2->left) &&
flipEquiv(root1->right, root2->right) ||
flipEquiv(root1->left, root2->right) &&
flipEquiv(root1->right, root2->left));
}
};