-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeLevelOrderTraversal.py
More file actions
36 lines (30 loc) · 1001 Bytes
/
Copy pathbinaryTreeLevelOrderTraversal.py
File metadata and controls
36 lines (30 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@FileName : binaryTreeLevelOrderTraversal.py
@Author : 56
@Date : 2020/5/13
@Description: https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
"""
from typing import List
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if root is None: return []
result: List[List[int]] = []
queue: List[TreeNode] = [root]
while len(queue) != 0:
values: List[int] = []
newQueue: List[TreeNode] = []
for node in queue:
values.append(node.val)
if node.left is not None: newQueue.append(node.left)
if node.right is not None: newQueue.append(node.right)
result.append(values)
queue = newQueue
return result