Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<int>> zigzagLevelOrder(TreeNode* root)
- {
- vector<vector<int>> zigzag;
- vector<int> temporal;
- bool valor = true, cambio = true;
- if(root==NULL)
- {
- return zigzag;
- }
- int posicion = 0;
- stack<TreeNode*> s;
- queue<TreeNode*> q;
- q.push(root);
- while(!q.empty()||!s.empty())
- {
- if(valor)
- {
- temporal.push_back(q.front()->val);
- if(q.front()->left!=NULL)
- {
- s.push(q.front()->left);
- }
- if(q.front()->right!=NULL)
- {
- s.push(q.front()->right);
- }
- q.pop();
- if(q.empty())
- {
- valor = false;
- }
- }
- else
- {
- temporal.push_back(s.top()->val);
- if(s.top()->left!=NULL)
- {
- q.push(s.top()->left);
- }
- if(s.top()->right!=NULL)
- {
- q.push(s.top()->right);
- }
- s.pop();
- if(s.empty())
- {
- valor = true;
- }
- }
- if(cambio!=valor)
- {
- cambio = !cambio;
- zigzag.push_back(temporal);
- temporal.clear();
- posicion++;
- }
- }
- return zigzag;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement