📘
Binary Tree Level Order Traversal
MediumTrees
Given a binary tree (level-order array), return the level order traversal as each level on a separate line.
Output each level space-separated.
Example:
Input:
3,9,20,null,null,15,7
Output:
3 9 20 15 7
Reasoning:
- The binary tree is constructed from the level-order array: the first element
3is the root, the next two elements9and20are its children, and the last two elements15and7are the children of20. - We start the level order traversal from the root
3, which is the first level and only contains the value3. - The next level consists of the root's children,
9and20, which are output as9 20. - The final level consists of the children of
20, which are15and7, output as15 7.
Constraints:
- 0 <= number of nodes <= 2000
- -1000 <= Node.val <= 1000
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.