BFS Level Order Traversal C++
Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;
# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\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    int level=0;\n    queue<TreeNode*>tobetraversed;\n    void Traverse(vector<vector<int>>&ans)\n    {\n        int thisiteration=tobetraversed.size();\n        vector<int>values;\n        while(thisiteration--)\n        {\n            if(tobetraversed.front()==NULL)\n            {\n                tobetraversed.pop();\n                continue;\n            }\n            if(tobetraversed.front()->left!=NULL)\n            {\n                tobetraversed.push(tobetraversed.front()->left);\n            }\n            if(tobetraversed.front()->right!=NULL)\n            {\n                tobetraversed.push(tobetraversed.front()->right);\n            }\n            values.push_back(tobetraversed.front()->val);\n            tobetraversed.pop();\n        }\n        if(level%2==1)\n        {\n            reverse(values.begin(),values.end());\n        }\n        level++;\n        ans.push_back(values);\n        if(tobetraversed.size()!=0)\n        {\n            Traverse(ans);\n        }\n        return;\n    }\n    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n        vector<vector<int>>ans;\n        if(root==NULL)\n        {\n            return ans;\n        }\n        tobetraversed.push(root);\n        Traverse(ans);\n        return ans;\n    }\n};\n```
['C++']