103-Binary Tree Zigzag Level Order Traversal
3
/ \
9 20
/ \
15 7class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector< vector<int>> result;
if(!root) {
return result;
}
queue <TreeNode *> q;
int idx = 0;
q.push(root);
int direction = 1; // -1 : 왼쪽시작, 1 : 오른쪽 시작
vector<int>temp;
temp.push_back(root->val);
result.push_back(temp);
while(!q.empty()) {
vector<int> subResult;
int size = q.size();
for(int i = 0; i < size; i++) {
root = q.front();
q.pop();
// 오른시작
if(direction == 1) {
if(root->right != NULL) {
q.push(root->right);
subResult.push_back(root->right->val);
}
if(root->left != NULL) {
q.push(root->left);
subResult.push_back(root->left->val);
}
// 왼 시작
} else {
if(root->right != NULL) {
q.push(root->right);
subResult.push_back(root->right->val);
}
if(root->left != NULL) {
q.push(root->left);
subResult.push_back(root->left->val);
}
}
}
// 무조건 왼->오로 노출
if(direction == -1) {
reverse(subResult.begin(), subResult.end());
}
if(subResult.size() >= 1) {
result.push_back(subResult);
}
direction *= -1;
}
return result;
}
};Last updated
Was this helpful?