Python short solution with recursion
First put in a dict with level number. And after extraction convert it to list.\n\'\'\'\nclass Solution:\n    def levelOrder(self, root: Optional[TreeNode]) -> 
First put in a dict with level number. And after extraction convert it to list.\n\'\'\'\nclass Solution:\n    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n        \n        if not root:\n            return []\n    \n        result = {}\n        \n        def traverse(node, level):\n            if node:\n                if level not in result:\n                    result[level] = []\n                result[level].append(node.val)\n                \n                traverse(node.left, level+1)\n                traverse(node.right, level+1)\n    \n        traverse(root, 0)\n        \n        return [result[i] for i in result.keys()]\n\'\'\'
['Recursion', 'Python']