. - 力扣(LeetCode)
![](https://i-blog.csdnimg.cn/direct/8702c57ed57b4094a01082ad79b95992.png)
![](https://i-blog.csdnimg.cn/direct/432074a2a561421d97fbe18239e6f320.png)
![](https://i-blog.csdnimg.cn/direct/8eebb0ee8b57482facd868401d6e32a1.png)
![](https://i-blog.csdnimg.cn/direct/50c2c4537cda4afc838ed4165a82eff3.png)
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
return construct(nums, 0, nums.size() - 1);
}
TreeNode* construct(const vector<int>& nums, int left, int right) {
if (left > right) {
return nullptr;
}
int best = left;
for (int i = left + 1; i <= right; ++i) {
if (nums[i] > nums[best]) {
best = i;
}
}
TreeNode* node = new TreeNode(nums[best]);
node->left = construct(nums, left, best - 1);
node->right = construct(nums, best + 1, right);
return node;
}
};