# 二叉树的层序遍历

<p>给你一个二叉树，请你返回其按 <strong>层序遍历</strong> 得到的节点值。 （即逐层地，从左到右访问所有节点）。</p>

<p> </p>

<p><strong>示例：</strong><br />
二叉树：<code>[3,9,20,null,null,15,7]</code>,</p>

<pre>
    3
   / \
  9  20
    /  \
   15   7
</pre>

<p>返回其层序遍历结果：</p>

<pre>
[
  [3],
  [9,20],
  [15,7]
]
</pre>


## template

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []

        queue, res = [root], []
        while queue:
            size = len(queue)
            temp = []
            for i in range(size):
                data = queue.pop(0)
                temp.append(data.val)
                if data.left:
                    queue.append(data.left)
                if data.right:
                    queue.append(data.right)
            res.append(temp)
        return res

```

## 答案

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []

        queue, res = [root], []
        while queue:
            size = len(queue)
            temp = []
            for i in range(size):
                data = queue.pop(0)
                temp.append(data.val)
                if data.left:
                    queue.append(data.left)
                if data.right:
                    queue.append(data.right)
            res.append(temp)
        return res
```

## 选项

### A

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []

        queue, res = [root], []
        while queue:
            size = len(queue)
            temp = []
            for i in range(size):
                data = queue.pop(0)
                temp.append(data.val)
                queue.append(data.left)
                queue.append(data.right)
            res.append(temp)
        return res
```

### B

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []

        queue, res = [root], []
        while queue:
            size = len(queue)
            temp = []
            for i in range(size):
                data = queue.pop(0)
                temp.append(data.val)
                if data.left:
                    queue.append(data.left)
                if data.right:
                    queue.append(data.right)
                res.append(temp)
        return res
```

### C

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []

        queue, res = [root], []
        while queue:
            size = len(queue)
            temp = []
            for i in range(size):
                data = queue.pop(0)
                temp.append(data.val)
            if data.left:
                queue.append(data.left)
            if data.right:
                queue.append(data.right)
            res.append(temp)
        return res
```