leetcode(리트코드)3월29일 challenge971-Flip Binary Tree To Match Preorder Traversal
leetcode(리트코드)3월29일 challenge971-Flip Binary Tree To Match Preorder Traversal
leetcode March 29일 - Flip Binary Tree To Match Preorder Traversal 문제입니다.
1. 문제
https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/
2. Input , Output
3. 분류 및 난이도
Medium 난이도입니다.
3월 28일자 챌린지 문제입니다.
4. 문제 해석
- 주어진 트리속에서 자식을 swap하여 preorder했을 때 voyage에 들어온 값과 맞으면 swap한 자식들을 모은 벡터를 리턴합니다.
5. code
c++
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
/**
* 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 check(TreeNode* root,vector<int>& voyage, int& count,vector<int>& result){
if(root==nullptr) return true;
if(root->val != voyage[count++]) return false;
auto left = root->left;
auto right = root->right;
if(left!=nullptr && left->val != voyage[count]){
result.push_back(root->val);
swap(left,right);
}
return check(left,voyage,count,result) && check(right,voyage,count,result);
}
vector<int> flipMatchVoyage(TreeNode* root, vector<int>& voyage) {
int count = 0;
vector<int> result;
if(check(root,voyage,count,result)){
return result;
}
return vector<int>() = {-1};
}
};
6. 결과 및 후기, 개선점
This post is licensed under CC BY 4.0 by the author.