Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Zig-Zag Level Order Traversal
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
vector<vector<int>> solve(TreeNode* root){
//CODE HERE
vector<vector<int>> ans;
queue<TreeNode*>q;
q.push(root);

int count =0;
if(root == NULL)
return ans;

while(1){
int size = q.size();
if(size == 0)
return ans;

vector<int>data;
count++ ;
while(size>0){
TreeNode* temp = q.front();
q.pop();

data.push_back(temp->val);
if(temp->left != NULL)
q.push(temp->left);
if(temp->right != NULL)
q.push(temp->right);
size--;

}
if(count%2==0)
reverse(data.begin(), data.end());
ans.push_back(data);
}
return ans;
}