C++ ✅ || Beats 100%💯 || Fast Solution🔥|| Simple Code🚀
🌟 Intuition"Let's go zigzag through the tree, level by level!"
This problem is like navigating a forest, but with a twist! Instead of moving straight, we need t
![Screenshot 2024-12-27 133942.png](https://assets.leetcode.com/users/images/6730777b-bde8-4e03-a62a-88363e92d832_1735287033.327401.png)

# 🌟 Intuition  
*"Let's go zigzag through the tree, level by level!"*  
This problem is like navigating a forest, but with a twist! Instead of moving straight, we need to alternate our direction as we go down the tree. At each level, the traversal should go left-to-right for one level, then right-to-left for the next. Think of it as weaving through the branches in a snake-like pattern! 🐍

# 🚀 Approach  
1. Perform a level order traversal (BFS) to capture the nodes at each level.  
2. Use a flag (`level % 2 == 0`) to determine if we need to reverse the order of the current level.  
   - Even levels are left-to-right.  
   - Odd levels are right-to-left.
3. Store nodes in a vector of vectors to keep track of each level's traversal.  

# 📊 Complexity  
- **Time Complexity:** $$O(n)$$ 🕒 — We visit each node once.  
- **Space Complexity:** $$O(n)$$ 📦 — The space required for the result and the recursive call stack is proportional to the number of nodes.

# 💻 Code  
```cpp
class Solution {
public:
    void solve(vector<vector<int>>& ans, TreeNode* temp, int level) {
        if (temp == NULL) return;
        if (ans.size() <= level) ans.push_back({});
        if (level % 2 == 0) ans[level].push_back(temp->val);
        else ans[level].insert(ans[level].begin(), temp->val);
        solve(ans, temp->left, level + 1);
        solve(ans, temp->right, level + 1);
    }

    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        solve(ans, root, 0);
        return ans;
    }
};
```
![upvote.jpeg](https://assets.leetcode.com/users/images/497d3107-f1ae-4d61-8553-d5f277772a44_1735287018.473409.jpeg)

['Tree', 'Breadth-First Search', 'Binary Tree', 'C++']