#C2800. Construct Maximum Binary Tree
Construct Maximum Binary Tree
Construct Maximum Binary Tree
Given an array of integers, construct the maximum binary tree according to the following rules:
- The root is the maximum number in the array.
- The left subtree is the maximum tree constructed from the elements to the left of the maximum number.
- The right subtree is the maximum tree constructed from the elements to the right of the maximum number.
For example, given the array \( [3, 2, 1, 6, 0, 5] \), the maximum binary tree is represented in level‐order as:
[6, 3, 5, None, 2, 0, None, None, 1]
The output should be a level-order traversal list of the tree where missing children are represented as None
.
inputFormat
The input is read from standard input. The first line contains a single integer ( n ) (the number of elements). The second line contains ( n ) space-separated integers.
outputFormat
Print the level-order traversal of the constructed maximum binary tree as a list. Missing children must be represented as None. The output format must exactly match the example.## sample
6
3 2 1 6 0 5
[6, 3, 5, None, 2, 0, None, None, 1]