👏🏻 Python | BFS Queue - 公瑾™ (102)
Binary Tree Level Order Traversal\n\nTime: O(N) | Space: O(size of return array + size of queue) -> Worst Case O(2N)\n\n\nqueue\u7684\u6982\u5FF5\u7528deque\u67
### Binary Tree Level Order Traversal\n```\nTime: O(N) | Space: O(size of return array + size of queue) -> Worst Case O(2N)\n```\n\nqueue\u7684\u6982\u5FF5\u7528deque\u6765\u5B9E\u73B0\uFF0C`popleft()` \u65F6\u95F4\u590D\u6742\u4E3AO(1)\u5373\u53EF\n\n\u5916\u56F4\u7684While\u7528\u6765\u5B9A\u4E49BFS\u7684\u7EC8\u6B62\u6761\u4EF6\uFF0C\u6240\u4EE5\u6211\u4EEC\u6700\u5F00\u59CBinitialize queue\u7684\u65F6\u5019\u53EF\u4EE5\u76F4\u63A5\u628Aroot\u653E\u8FDB\u53BB\n\u5728\u6BCF\u5C42\u7684\u65F6\u5019\uFF0C\u901A\u8FC7\u4E00\u4E2A`cur_level`\u8BB0\u5F55\u5F53\u524D\u5C42\u7684`node.val`\uFF0Csize\u7528\u6765\u8BB0\u5F55queue\u7684\u5728\u589E\u52A0\u5B50\u5B59node\u4E4B\u524D\u5927\u5C0F\uFF0C\u56E0\u4E3A\u4E4B\u540E\u6211\u4EEC\u4F1A\u5B9E\u65F6\u66F4\u65B0queue\u7684\u5927\u5C0F\u3002\n\u5F53\u6BCF\u6B21\u4ECEqueue\u4E2Dpop\u51FA\u6765\u7684\u8282\u70B9\uFF0C\u628A\u5B83\u7684\u5DE6\u53F3**\u5B50\u8282\u70B9**\u653E\u8FDBQueue\u4EE5\u540E\uFF0C\u8BB0\u5F97\u628A\u8282\u70B9\u672C\u8EAB\u7684\u7684value\u653E\u8FDB`cur_level`\n`for loop`\u7EC8\u6B62\u540E\uFF0C\u5C31\u53EF\u4EE5\u628A\u8BB0\u5F55\u597D\u7684\u6574\u5C42\u7684\u6570\u503C\uFF0C\u653E\u5165\u6211\u4EEC\u7684return\u6570\u7EC4\u91CC\u3002\n\n```python\nfrom collections import deque\nclass Solution:\n    def levelOrder(self, root):\n        if not root: return []\n        queue, res = deque([root]), []\n        \n        while queue:\n            cur_level, size = [], len(queue)\n            for i in range(size):\n                node = queue.popleft()\n                if node.left:\n                    queue.append(node.left)\n                if node.right:\n                    queue.append(node.right)\n                cur_level.append(node.val)\n            res.append(cur_level)\n        return res\n```\n\n\n# \u7C7B\u4F3C\u9898\u76EE:\n**LC 993**\n\u9898\u76EE\u94FE\u63A5: [Link](https://leetcode.com/problems/cousins-in-binary-tree/)\n\u7B54\u6848\u94FE\u63A5: [Link](https://leetcode.com/problems/cousins-in-binary-tree/discuss/257862/Python-BFS-%2B-Hash-tm-(993))\n
[]