Python short recursive and iterative solutions
When doing DFS recursively, add a variable to record the level information.\n\n\nclass Solution(object):\n    def levelOrder(self, root):\n        from collecti
When doing DFS recursively, add a variable to record the level information.\n\n```\nclass Solution(object):\n    def levelOrder(self, root):\n        from collections import deque\n        deque, ret = deque(), []\n        if root:\n            deque.append(root)\n        while deque:\n            level, size = [], len(deque)\n            for _ in range(size):\n                node = deque.popleft()\n                level.append(node.val)\n                if node.left:\n                    deque.append(node.left)\n                if node.right:\n                    deque.append(node.right)\n            ret.append(level)\n        return ret\n\t\t\n    def levelOrder(self, root):\n        res = []\n        self.dfs(root, 0, res)\n        return res\n        \n    def dfs(self, root, level, res):\n        if not root:\n            return \n        if len(res) < level+1:\n            res.append([])\n        res[level].append(root.val)\n        self.dfs(root.left, level+1, res)\n        self.dfs(root.right, level+1, res)\n```
['Depth-First Search', 'Python']