0102. Level Order Traversal
Medium | Tree + Traversal | 20 ms (99.85%), 14.6 MB (69.19%)
Source: LeetCode - Binary Tree Level Order Traversal GitHub: Solution / Performance
Given the root
of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
Instead of using Stack, for level-order, we need to use Queue.
Since level-order traversal starts from left to right, the left-most node will be the one pushed at first. On the other hand, the right-most node will be the one pushed at last.
Traversing from left to right means we need to pop nodes in a FIFO method, so Queue is exactly the data structure we need.
Last updated