1
+ // Runtime: 4 ms (Top 80.72%) | Memory: 13.8 MB (Top 73.01%)
1
2
class Solution {
2
3
public:
3
-
4
+
4
5
bool helper (int &ind, TreeNode *root, vector<int > &voyage, vector<int > &ans){
5
6
if (root == NULL || ind == voyage.size ()){
6
7
ind--;
7
8
return true ;
8
9
}
9
-
10
- // Not possible to create
10
+
11
+ // Not possible to create
11
12
if (root->val != voyage[ind]){
12
13
ans.clear ();
13
14
ans.push_back (-1 );
14
15
return false ;
15
16
}
16
-
17
+
17
18
// If voyage value not equal to its left child, then swap both childs and check
18
19
if (root->left && root->left ->val != voyage[ind+1 ]){
19
- TreeNode *temp = root->left ;
20
- root->left = root->right ;
21
- root->right = temp;
22
-
23
- // Pusing root into ans
24
- ans.push_back (root->val );
20
+ TreeNode *temp = root->left ;
21
+ root->left = root->right ;
22
+ root->right = temp;
23
+
24
+ // Pusing root into ans
25
+ ans.push_back (root->val );
25
26
}
26
-
27
+
27
28
return helper (++ind, root->left , voyage, ans) &&
28
29
helper (++ind, root->right , voyage, ans);
29
30
}
30
-
31
+
31
32
vector<int > flipMatchVoyage (TreeNode* root, vector<int >& voyage) {
32
33
int ind = 0 ;
33
34
vector<int > ans;
34
35
helper (ind, root, voyage, ans);
35
36
return ans;
36
37
}
37
- };
38
+ };
0 commit comments