C++| Level Order Traversal | Beats 100% | BFS
Intuition\n Describe your first thoughts on how to solve this problem. \nSo Do Simple Level order but alternately revrse the elemnts of level.\n- Either we can 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSo Do Simple Level order but alternately revrse the elemnts of level.\n- Either we can  reverse the vector storing the elemnts f that level\n- Or we just insert from last index so that it will be in reverse order itself.(More Efficient)\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Level Order Traversal: For More Understanding  [Refer Here](https://leetcode.com/problems/binary-tree-level-order-traversal/solutions/3196962/c-bfs-dfs-on-explained/)\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n**Level Order Traversal by Reversing vector alternately**\n```\nclass Solution {\npublic:\n    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n        vector<vector<int>>ans;\n        if(!root)return ans;\n        queue<TreeNode*>q;\n        q.push(root);\n        int i=0;\n        while(!q.empty()){\n           int sz=q.size();\n           vector<int>v;\n           while(sz--){\n               TreeNode * f=q.front();\n               v.push_back(q.front()->val);\n               q.pop();\n               if(f->left)q.push(f->left);\n               if(f->right)q.push(f->right);\n\n           }\n           if(i++%2)\n           reverse(v.begin(),v.end());\n           ans.push_back(v);\n\n        }\n        return ans;\n    \n    }\n};\n```\n**More Optimized Level Order Wihtout Reversing Vector**\n```\nvector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n        vector<vector<int>>ans;\n        if(!root)return ans;\n        queue<TreeNode*>q;\n        q.push(root);\n        int i=0;\n        while(!q.empty()){\n           int sz=q.size();\n           vector<int>v(sz);\n           int j=0,n=sz;\n           while(sz--){\n               TreeNode * f=q.front();\n               q.pop();\n               if(i%2==0)\n               v[j]=f->val;\n               else v[n-j-1]=f->val;\n               j++;\n               if(f->left)q.push(f->left);\n               if(f->right)q.push(f->right);\n           }\n           i++;\n           ans.push_back(v);\n\n        }\n        return ans;\n    \n    }\n```\n# Upvote If Help full
['Tree', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'C++']