Easy To Understand || Well Commented || Using Queue
\n# Complexity\n- Time complexity:\nO(n) \n\n- Space complexity:\n O(n)\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     in
\n# Complexity\n- Time complexity:\n$$O(n)$$ \n\n- Space complexity:\n $$O(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    vector<vector<int>> levelOrder(TreeNode* root) {\n        //level order traversal \n        vector<vector<int>>ans;\n        queue<TreeNode*>q;//stores nodes\n        if(root==NULL){\n            return ans; //i.e empty\n        }\n        //else insert root  in queue\n        q.push(root);\n\n        while(!q.empty()){\n            vector<int>level;\n            int size_to_traverse = q.size();//traversing till size of each level\n\n            for(int i=0;i<size_to_traverse;i++){\n                //see if left and right exist if yes insert that nodes in queue\n                TreeNode*node = q.front();\n                q.pop();//remove from queue \n                if(node->left!=NULL){\n                    q.push(node->left);\n                }\n                if(node->right!=NULL){\n                    q.push(node->right);\n                }\n            //now,insert all level nodes in level vector \n            level.push_back(node->val);\n            }\n       //store level vector in ans now --> as nodes get inserted level vise \n       ans.push_back(level);\n        } \n        return ans;\n    }\n};\n```\n```\nIf you find this solutions to be helpful do upvote..It keeps me motivated :)\n```
['Tree', 'Queue', 'Binary Tree', 'C++']