| 40 | from collections import deque |
| 41 | |
| 42 | class Solution(object): |
| 43 | def levelOrder(self, root): |
| 44 | """ |
| 45 | :type root: TreeNode |
| 46 | :rtype: List[List[int]] |
| 47 | """ |
| 48 | if not root: |
| 49 | return [] |
| 50 | |
| 51 | result = [] |
| 52 | |
| 53 | temp = deque([root]) |
| 54 | next_temp = deque() |
| 55 | _result = [] |
| 56 | while 1: |
| 57 | if temp: |
| 58 | node = temp.popleft() |
| 59 | _result.append(node.val) |
| 60 | if node.left: |
| 61 | next_temp.append(node.left) |
| 62 | |
| 63 | if node.right: |
| 64 | next_temp.append(node.right) |
| 65 | else: |
| 66 | result.append(_result) |
| 67 | _result = [] |
| 68 | temp = next_temp |
| 69 | next_temp = deque() |
| 70 | |
| 71 | if not temp and not next_temp: |
| 72 | if _result: |
| 73 | result.append(_result) |
| 74 | return result |
| 75 | |
| 76 |
nothing calls this directly
no outgoing calls
no test coverage detected