Skip to content
BASA DSA

Binary Tree Level Order Traversal

MediumTrees

Return binary tree values grouped by depth, from the root down and from left to right within each level.

Example 1

Argument 1 tree: level-order input; omitted children are null.Argument 1 tree: level-order input; omitted children are null. Vertices: node 0: 1, node 1: 2, node 2: 3, node 3: 4. Edges: node 0 (1) — node 1 (2) left child; node 0 (1) — node 2 (3) right child; node 1 (2) — node 3 (4) right child.1234
Argument 1 tree: level-order input; omitted children are null.

Input
root = [1,2,3,null,4]
Output
[[1],[2,3],[4]]

Explanation

Read root [1], children [2,3], then the only grandchild [4].

Example 2

Argument 1 tree: empty tree (null root).
Input
root = []
Output
[]

Explanation

An empty tree has no levels.

Example 3

Argument 1 tree: level-order input; omitted children are null.Argument 1 tree: level-order input; omitted children are null. Vertices: node 0: 8. Edges: none.8
Argument 1 tree: level-order input; omitted children are null.

Input
root = [8]
Output
[[8]]

Explanation

A leaf gives one level containing 8.

Constraints

  • 0 <= number of nodes <= 2000
  • Trees are breadth-first arrays with null holes

Function contractlevelOrderArguments: tree
Loading draft…
SolutionRuns on your device
Loading draft…