DFS super simple faster than 92%
Intuition\nFirst thought when looking at tree levels is using BFS, but in this case there is no benefit to doing so since we don\'t care about the order, we jus
# Intuition\nFirst thought when looking at tree levels is using BFS, but in this case there is no benefit to doing so since we don\'t care about the order, we just want a result with all the elements of every level. Moreover, doing so with BFS will complicate things because we need to indicate we reached the end of a level\n\n# Approach\nWith simple DFS, create a util function that take a level argument and every recursive call incremenets it by one. Adding to the result by level index and if an array doesn\'t exist for that level, create it and assign the current node to it.\n\n# Complexity\n- Time complexity:\nO(n) since we visit every element of the tree once\n\n- Space complexity:\nO(n) since we create an array with the same size of the original tree even though it\'s modeled as a two dimensional array\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * class TreeNode {\n *     val: number\n *     left: TreeNode | null\n *     right: TreeNode | null\n *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n *         this.val = (val===undefined ? 0 : val)\n *         this.left = (left===undefined ? null : left)\n *         this.right = (right===undefined ? null : right)\n *     }\n * }\n */\n\nfunction levelOrder(root: TreeNode | null): number[][] {\n    var response = []\n\n    const util = (root: TreeNode | null, level: number) => {\n        if(root == null) return\n\n        if(response[level]) {\n            response[level].push(root.val)\n        } else {\n            response[level] = [root.val]\n        }\n        \n        util(root.left, level + 1)\n        util(root.right, level + 1)\n    }\n\n    util(root, 0)\n    return response\n};\n```
['TypeScript']