102. Binary Tree Level Order Traversal
https://leetcode.com/problems/binary-tree-level-order-traversal/
Intuition
Level order requires access to all nodes which are 1 level away from parent.
Essentially one edge away from parent - very similar to BFS in a graph
We need to return all nodes L to R in the order they are encountered when traversing a level. Queue is appropriate data structure as it has FIFO behavior
Implementation is a standard BFS traversal using queue
----
Time - O(n)
Space - O(log(n))
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
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] ]----
Intuition
Level order requires access to all nodes which are 1 level away from parent.
Essentially one edge away from parent - very similar to BFS in a graph
We need to return all nodes L to R in the order they are encountered when traversing a level. Queue is appropriate data structure as it has FIFO behavior
Implementation is a standard BFS traversal using queue
----
Time - O(n)
Space - O(log(n))