PuTtA EaSY Solution C++ ✅ | Beats 100% 🔥Runtime 0ms 🔥| Funday
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nUsing Queue , placing t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing Queue , placing the values at right indexes .\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n        queue<TreeNode*> q;\n        q.push(root);\n        vector<vector<int>> ans; // answer vector\n        bool left = false;\n        if(root == NULL) return ans; //if tree is empty\n        while(!q.empty()){ //level order traversal\n            int n= q.size();\n            vector<int> ans1(n); \n            for(int i=0;i<n;i++){\n                TreeNode* temp = q.front();\n                if(left == false) ans1[i] = temp->val;\n                else ans1[n-i-1] = temp->val;\n                q.pop();\n                // pushing next level into queue\n                if(temp->left != NULL) q.push(temp->left);\n                if(temp->right != NULL) q.push(temp->right);\n            }\n            left = !left; // changing the zig-Zag order\n            ans.push_back(ans1);\n        }\n        return ans;\n    }\n};\n```\n![Screenshot (8).png](https://assets.leetcode.com/users/images/90f6f168-24b2-4a18-8cb7-2521712a6b38_1676804866.9230862.png)\n
['Tree', 'Breadth-First Search', 'Binary Tree', 'C++']