Easy || 100% || Fully Explained || Java, C++, Python, Javascript, Python3 || BFS || DFS
Java Solution (Breadth First Search Approach):\n\n// Time Complexity : O(n)\n// Space Complexity : O(n)\nclass Solution {\n    public List<List<Integer>> levelO
# **Java Solution (Breadth First Search Approach):**\n```\n// Time Complexity : O(n)\n// Space Complexity : O(n)\nclass Solution {\n    public List<List<Integer>> levelOrder(TreeNode root) {\n        // Create an array list to store the output result...\n        List<List<Integer>> output = new ArrayList<>();\n        // Base case: If the tree is empty...\n\t\tif (root == null)  return output;\n\t    // Initialize a queue to store the nodes on the same level & add root in it...\n\t\tLinkedList<TreeNode> q = new LinkedList<TreeNode>();\n\t\tq.add(root);\n        // Traverse a loop untill the queue becomes empty...\n\t\twhile (!q.isEmpty()) {\n            // Denotes the number of elements on that level...\n            int size = q.size();\n            // A temporary list to store all the left and right child for all the node in the level...\n\t\t\tArrayList<Integer> temp = new ArrayList<>(size);\n\t\t\tfor (int idx = 0; idx < size ; idx++) {\n                // Initialize a treenode as the popped element of the queue & store the value of the new treenode to temp...\n\t\t\t\tTreeNode node = q.remove();\n\t\t\t\ttemp.add(node.val);\n                // Store all the nodes of next level...\n                // Add left and right child if they are not None...\n\t\t\t\tif (node.left != null)\n\t\t\t\t\tq.add(node.left);\n\t\t\t\tif (node.right != null)\n\t\t\t\t\tq.add(node.right);\n\t\t\t}\n            // Add the temp to output result...\n\t\t\toutput.add(temp);\n\t\t}\n\t\treturn output;       // Return the output result...\n    }\n}\n```\n\n# **C++ Solution (Depth First Search Approach):**\n```\n// Time Complexity : O(n)\n// Space Complexity : O(n)\nclass Solution {\n    // Create a vector to store the output result...\n    vector<vector<int>> output;\n    // Take a helper function...\n    void Traverse(TreeNode *root, int level){\n        // Base case: if root is empty...\n        if(root == NULL) return;\n        // If it is the first time we reached this level...\n        // Pushes an empty vector in...\n        if(output.size() == level)\n            output.push_back(vector<int>());\n        // Pushes the value of root into output[level]...\n        output[level].push_back(root->val);\n        // Recursively call the helper function on the left branch after increasing level...\n        Traverse(root->left, level + 1);\n        // Recursively call the helper function on the right branch with increased level...\n        Traverse(root->right, level + 1);\n    }\npublic:\n    vector<vector<int>> levelOrder(TreeNode *root) {\n        // Passing root to populate...\n        Traverse(root, 0);\n        return output;\n    }\n};\n```\n\n# **Python/Python3 Solution (Breadth First Search Approach):**\n```\n# Time Complexity : O(n)\n# Space Complexity : O(n)\nclass Solution(object):\n    def levelOrder(self, root):\n        # Base case: If the tree is empty...\n        if root is None: return []\n        # Initialize a queue to store the nodes on the same level & create a list to store the output result...\n        q, output = collections.deque([root]),[]\n        # Traverse a loop untill the queue becomes empty...\n        while q:\n            # Denotes the number of elements on that level...\n            size = len(q)\n            # A temporary list to store all the left and right child for all the node in the level...\n            temp=[]\n            while size > 0:\n                node = q.popleft()\n                # Store the value of node to temp...\n                temp.append(node.val)\n                # Store all the nodes of next level...\n                # Add left and right child if they are not None...\n                if node.left:\n                    q.append(node.left)\n                if node.right:\n                    q.append(node.right)\n                size-=1\n            # Add the temp to output result...\n            output.append(temp)\n        return output       # Return the output result...\n```\n                        \n# **JavaScript Solution (Breadth First Search Approach):**\n```\n// Time Complexity : O(n)\n// Space Complexity : O(n)\nvar levelOrder = function(root) {\n    // Initialize a queue to store the nodes on the same level & add root in it...\n    let q = [root]\n    // Create an array list to store the output result...\n    output = []\n    // Traverse a loop untill the queue becomes empty...\n    while (q[0]) {\n        // Denotes the number of elements on that level...\n        let size = q.length\n        // A temporary list to store all the left and right child for all the node in the level...\n        temp = []\n        for (let i = 0; i < size; i++) {\n            let node = q.shift()\n            // Store the value of node to temp...\n            temp.push(node.val)\n            // Store all the nodes of next level...\n            // Add left and right child if they are not None...\n            if (node.left) q.push(node.left)\n            if (node.right) q.push(node.right)\n        }\n        // Add the temp to output result...\n        output.push(temp)            \n    }\n    return output       // Return the output result...\n};\n```\n**I am working hard for you guys...\nPlease upvote if you found any help with this code...**
['Tree', 'Depth-First Search', 'Breadth-First Search', 'C', 'Python', 'Java', 'Python3', 'JavaScript']