Code Logo

Tree Level-Order Traversal

Published at25 Jul 2026
Tree Traversal Easy 2 views
Like0

Given an array representing a binary tree in level-order format (root at index 0, children at 2i+1 and 2i+2), return the level-order traversal as an array.

Level-order traversal visits nodes row by row, from left to right. In the array representation, this is simply the array itself since the array IS the level-order representation.

Example Input & Output

Example 1
Input
[5,10,15,20]
Output
[5,10,15,20]
Explanation

Four nodes

Example 2
Input
[1,2,3,4,5]
Output
[1,2,3,4,5]
Explanation

Level order

Example 3
Input
[]
Output
[]
Explanation

Empty

Example 4
Input
[10]
Output
[10]
Explanation

Single

Example 5
Input
[1,2,3,4,5,6,7]
Output
[1,2,3,4,5,6,7]
Explanation

Full tree

Algorithm Flow

Recommendation Algorithm Flow for Tree Level-Order Traversal
Recommendation Algorithm Flow for Tree Level-Order Traversal

Solution Approach

function solution(arr) {
  return arr.slice();
}

Best Answers

java
class Solution {
    public int[] solution(int[] nums) {
        return java.util.Arrays.copyOf(nums,nums.length);
    }
}