Binary Tree Level Order Traversal
Binary Tree Level Order Traversal
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution(object): def levelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ if root is None: return [] results = [] q = [] def helper(root): q.append(root) while len(q) > 0: temp = [] for i in range(len(q)): node = q.pop(0)
temp.append(node.val) if node.left is not None: q.append(node.left) if node.right is not None: q.append(node.right)
results.append(temp) return helper(root) return results
temp.append(node.val) if node.left is not None: q.append(node.left) if node.right is not None: q.append(node.right)
results.append(temp) return helper(root) return results