Question Link:
https://leetcode.com/problems/binary-tree-level-order-traversal/
Traverse the tree level by level using BFS method.
[-]
Python code accepted by LeetCode OJ
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param root, a tree node
# @return a list of lists of integers
def levelOrder(self, root):
"""
BFS from the root.
For each level, insert a list of values into the result
"""
res = []
if not root:
return res
q = [root]
while q:
new_q = []
res.append([n.val for n in q])
for node in q:
if node.left:
new_q.append(node.left)
if node.right:
new_q.append(node.right)
q = new_q
return res