Medium

Len Chen
1 min readSep 24, 2018

Problem

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
]

Solution

By using a queue to do BFS approach from root, it’s easy.

Complexity

Because each node will be only visited once, it’s time complexity is O(n) if n denotes to count of nodes in this tree. And because the queue should have ability to save all leaves in this tree, it’s space complexity is O(n) as well.

--

--

Len Chen
Len Chen

No responses yet