-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path971.cpp
More file actions
37 lines (35 loc) · 882 Bytes
/
Copy path971.cpp
File metadata and controls
37 lines (35 loc) · 882 Bytes
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
class Solution {
public:
vector<int> nodes;
int index;
vector<int> voyage;
bool match = true;
vector<int> flipMatchVoyage(TreeNode *root, vector<int> &voyage) {
if (!root || voyage.size() == 0)
return {};
this->voyage = voyage;
nodes.clear();
index = 0;
helper(root);
if (!match)
return {-1};
return nodes;
}
void helper(TreeNode *root) {
if (!root)
return;
if (root->val != voyage[index] || !match) {
match = false;
return;
}
++index;
if (root->left && root->left->val != voyage[index]) {
nodes.push_back(root->val);
helper(root->right);
helper(root->left);
} else {
helper(root->left);
helper(root->right);
}
}
};