> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/654-maximum-binary-tree.md).

# 654 Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

The root is the maximum number in the array. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number. Construct the maximum tree by the given array and output the root node of this tree.

Example 1:

```
Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:

      6
    /   \
   3     5
    \    / 
     2  0   
       \
        1
```

Note: The size of the given array will be in the range \[1,1000].

**The Idea:** The problem initially seemed intimidating, but narrowing it down to sub problems simplifies it greatly. Using iterators for the array is a natural way to doing things, since it cleanly defines the bounds of our start and end position of `nums`. Assuming the array is not empty, we begin with the maximum element in the array as the root. The left and right children of the root will then have the maximum element to the left and right of the maximum element respectively. If `start == end`, then we know the array is empty and therefore contains no maximum element, so return nullptr.

**Complexity:** O(n^2) time and O(n) space

```cpp
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    return buildTree(nums.begin(), nums.end() - 1);         
}

template<typename iter>
TreeNode* buildTree(iter start, iter end) {
    if (start <= end) {
        auto max_iter = max_element(start, end + 1);
        TreeNode *root = new TreeNode(*max_iter);
        root->left = buildTree(start, max_iter - 1);
        root->right = buildTree(max_iter + 1, end);
        return root;
    }
    else return nullptr;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/654-maximum-binary-tree.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
