/102. Binary Tree Level Order Traversal

102. Binary Tree Level Order Traversal

Medium
Trees72.3% acceptance

Given the root node of a binary tree, return a list of lists containing the values of the nodes at each depth level, traversed from left to right for each level.

Example 1

Input: TreeNode(5, TreeNode(2), TreeNode(8, TreeNode(7), TreeNode(10)))

Output: [[5],[2,8],[7,10]]

Explanation: Root is 5, level 1 is [2,8], level 2 is [7,10].

Example 2

Input: TreeNode(4, None, TreeNode(6, TreeNode(5), None))

Output: [[4],[6],[5]]

Explanation: Root is 4, right child 6, left child of 6 is 5.

Example 3

Input: None

Output: []

Explanation: Empty tree returns empty list.

Constraints

  • The number of nodes in the binary tree is in the range [0, 2000].
  • Each node's value is an integer in the range [-1000, 1000].
Python (current runtime)

Case 1

Input: TreeNode(1, TreeNode(2, TreeNode(3)), TreeNode(4))

Expected: [[1],[2,4],[3]]

Case 2

Input: TreeNode(10, TreeNode(5), None)

Expected: [[10],[5]]

Case 3

Input: TreeNode(7)

Expected: [[7]]

Case 4

Input: TreeNode(9, TreeNode(8, None, TreeNode(6)), TreeNode(10))

Expected: [[9],[8,10],[6]]