102_Binary Tree Level Order Traversal
102. Binary Tree Level Order Traversal
Level: medium
Tag: tree, breadth-first search, deepth-first search
Question
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).Example 1
Input:
3
/ \
9 20
/ \
15 7
Output:
[
[3],
[9,20],
[15,7]
]Idea 1 (breadth-first search)
For each level, find all the node value, append to the result as a list.
Have a temporary list to store all the left and right child for all the node in the level.
Solution 1: breadth-first search
Time:
Space:
Solution 2: deepth-first search (harder to understand)
Time:
Space:
Last updated
Was this helpful?