Binary Tree Level Order Traversal
You get the root of a binary tree, possibly none. Return its values level by level: a list per level, from the top down and left to right within a level. Our example is the tree [7, 4, 9, 1, none, 6, 12, none, 3, 8] in level order — the answer is [[7], [4, 9], [1, 6, 12], [3, 8]].
- input
root: a binary tree, written as on LeetCode: its values level by level, left to right,nullfor a missing child.- returns
- Its values level by level: a list per level, from the root down, each one left to right;
[]for an empty tree. - constraints
- 0 ≤ the number of nodes ≤ 2000
- −1000 ≤ Node.val ≤ 1000
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
function levelOrder(root) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more breadth-first search (bfs) problems
- 637Average of Levels in Binary TreeEasysolve
- 199Binary Tree Right Side ViewMediumsolve
- 994Rotting OrangesMediumsolve
- 1091Shortest Path in Binary MatrixMedium
- 841Keys and RoomsMediumsolve
- 117Populating Next Right Pointers in Each Node IIMediumsolve
- 103Binary Tree Zigzag Level Order TraversalMediumsolve
- 909Snakes and LaddersMediumsolve
- 433Minimum Genetic MutationMediumsolve
- 127Word LadderHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.