C++ Simple Solution || Space and time less than 82.08% || BFS
In this question , we again use BFS for level order traversal , and as we can predict from output that levels on odd layers are being reversed . So thats the te
In this question , we again use **BFS** for level order traversal , and as we can predict from output that levels on odd layers are being reversed . So thats the technique we have used here.\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n        queue<TreeNode *> q;    //Initialising queue , (for bfs mostly we use queue) \n        vector<vector<int>> ans;    //ans 2D vector\n        int c=0;    //count variable\n        if(!root)\n            return ans; //To handle the edge test case i.e. if NULL tree is provided\n        q.push(root);   //now we push the whole root node in the queue\n        while(!q.empty())\n        {\n            int s = q.size();   //we take the size beforehand so that the changes made inside the loop doesnt affect , how many times it will run\n            vector<int> vec;    //1D vector to store values \n            c++;    //we increase count vector every time we go to one layer deep \n            for(int i=1;i<=s;i++)\n            {\n                TreeNode *t = q.front();        \n                q.pop();\n                vec.push_back(t->val);  //we push the value of the current node to the 1D vector\n                if(t->left)\n                    q.push(t->left);    //if the left node of the root node is present , then push the left node in the queue\n                if(t->right)\n                    q.push(t->right);   //if the right node of the root node is present , then push the right node in the queue\n            }\n            if(c%2==0)  \n                reverse(vec.begin(),vec.end()); //now , we took the count variable because there was a pattern in the output that the 1D vector in the 2D vector at even counts are reversed and thats how it was zigzag , so we check the condition and reverse it. \n            ans.push_back(vec); //here we push the 1D vector in the ans.\n        }\n        return ans; //returning the 2D vector as our ans \n    }\n};\n```\n**IF you LIKE the solution , DO UPVOTE\nHAPPY CODING;**
['Breadth-First Search', 'C', 'C++']