BFS C++ 0ms(simple solution)
This is not the best solution to this problem. This is just an approach in which I am using a bool variable for the zigzag traversal along with an eliminator fo
This is not the best solution to this problem. This is just an approach in which I am using a bool variable for the zigzag traversal along with an eliminator for clearing out temp before reaching next level.\n \n    vector<vector<int>> zigzagLevelOrder(TreeNode* root)\n     {\n        \n\t\tif(root == NULL)\n            return {};\n\t\t\t\n        vector<vector<int> > ans;\n        deque<TreeNode*> deque;\n        TreeNode* eliminator = new TreeNode(404);\n        bool right = true;\n        vector<int> temp;\n        \n        deque.push_back(root);\n        deque.push_back(eliminator);\n        \n        while(deque.size() > 1)\n        {   \n            if(right)\n            {\n                TreeNode* front = deque.front();\n                \n                if(front == eliminator)\n                {\n                    ans.push_back(temp);\n                    right = !right;\n                    temp.clear();\n                }\n                \n                else\n                {\n                    temp.push_back(front->val);\n                    deque.pop_front(); \n                    \n                    if(front->left)\n                       deque.push_back(front->left);\n                    if(front->right)\n                       deque.push_back(front->right);      \n                }\n            }\n            \n            else\n            {\n                TreeNode* back = deque.back();\n                \n                if(back == eliminator)\n                {\n                    ans.push_back(temp);\n                    right = !right;\n                    temp.clear();\n                }\n                \n                else\n                {\n                    temp.push_back(back->val);\n                    deque.pop_back();\n                    \n                    if(back->right)\n                       deque.push_front(back->right); \n                    if(back->left)\n                       deque.push_front(back->left);\n                }\n            }\n        }\n        ans.push_back(temp);\n        return ans;\n    }
[]