✔️ C++  |  PYTHON  |  JAVA  |  EXPLAINED  ||  ; ]
UPVOTE IF HELPFuuL\n\nLevel Order Traversal -> All nodes at i level should be listed/traversed prior to visiting any nodes in next level.\n\n\n\nAPPROACH\n\nLet
**UPVOTE IF HELPFuuL**\n\n*Level Order Traversal* -> All nodes at ```i``` level should be listed/traversed prior to visiting any nodes in next level.\n![image](https://assets.leetcode.com/users/images/5e4f21d3-3f1f-44d4-b674-c3d2c8f8896e_1657679591.4317877.png)\n\n\n**APPROACH**\n\nLet presume we are at ```i``` level. To traverse this level, we need all pointers that point to nodes at ```i``` level.\n\n**When at ```i``` level**, we will have a list of node pointers at that level. -> AT starting we will just have root in that list.\n\n* Create a new vector/array, and **add values of all nodes to it**. Add this vector/array to **ANSWER**\n* Alongwithit keep **storing the left and right child** of that node in a seperate vector/array. For traversing next LEVEL\n* Resucively call for the next level for the vector/array created by adding children.\n* Recursion stops when list containing nodes is **EMPTY**\n\n**UPVOTE IF HELPFuuL**\n**C++**\n```\nclass Solution {\npublic:\n    \n    vector<vector<int>> solve(vector<TreeNode*> k, vector<vector<int>> res){\n        if (k.size()==0) return res;\n        \n        vector<TreeNode*> newk;\n        vector<int> lvl;\n        \n        for (int i=0; i < k.size(); i++){\n            lvl.push_back( k[i] -> val );\n            if ( k[i] -> left ) newk.push_back( k[i] -> left);\n            if ( k[i] -> right ) newk.push_back( k[i] -> right);\n        }\n        \n        res.push_back(lvl);\n        return solve(newk, res);\n        \n    }   \n    \n    vector<vector<int>> levelOrder(TreeNode* root) {\n        \n        vector<TreeNode*> k{root};\n        vector<vector<int>> res;\n        \n        if (root==NULL) return res;\n        \n        return solve(k,res);\n        \n    }\n};\n```\n**PYTHON**\n```\nclass Solution:\n    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n        \n        def solve(k):\n            if len(k)==0:\n                return\n            \n            ans , newk= [], []\n            for node in k:\n                ans.append( node.val )\n                \n                if node.left:\n                    newk.append(node.left)\n                if node.right:\n                    newk.append(node.right)\n            \n            res.append(ans)\n            solve( newk )\n            return\n        \n        res = []\n        if root:\n            solve( [root] )\n        return res\n```\n**JAVA**\n```\npublic class Solution {\n    public List<List<Integer>> levelOrder(TreeNode root) \n    {\n        Queue<TreeNode> queue = new LinkedList<TreeNode>();\n        List<List<Integer>> res = new LinkedList<List<Integer>>();\n        \n        if(root == null) return res;\n        \n        queue.offer(root);\n        while(!queue.isEmpty())\n        {\n            int levelNum = queue.size();\n            List<Integer> subans = new LinkedList<Integer>();\n            for(int i=0; i<levelNum; i++) \n            {\n                if(queue.peek().left != null) queue.offer(queue.peek().left);\n                if(queue.peek().right != null) queue.offer(queue.peek().right);\n                subans.add(queue.poll().val);\n            }\n            res.add(subans);\n        }\n        return res;\n    }\n}\n```\n\n![image](https://assets.leetcode.com/users/images/62b51eae-3415-4c46-948d-5db6629844a9_1657712988.953082.jpeg)\n
['C', 'Interactive', 'Python', 'C++', 'Java']