-
Notifications
You must be signed in to change notification settings - Fork 481
/
0971.cpp
32 lines (27 loc) · 789 Bytes
/
0971.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
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage)
{
pos = 0;
return traverse(root, voyage) ? flips : vector<int>() = {-1};
}
private:
vector<int> flips;
int pos;
bool traverse(TreeNode* root, vector<int>& voyage)
{
if (root == nullptr) return true;
if (root->val != voyage[pos++]) return false;
auto l = root->left, r = root->right;
if (l != nullptr && l->val != voyage[pos])
{
flips.push_back(root->val);
swap(l, r);
}
return traverse(l, voyage) && traverse(r, voyage);
}
};