102. Binary Tree Level Order Traversal
[Medium] “Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level)."
Note: old solution, should review (TODO:
)
Python3:
# 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]]:
result = []
if not root:
current = []
else:
current = [root]
while current != []:
result.append([node.val for node in current])
new = []
for node in current:
if node.left:
new.append(node.left)
if node.right:
new.append(node.right)
current = new
return result