懒得写今天的,挑一个 binary tree
654. Maximum Binary Tree
写一写根本就变成练习 C++ container
class Solution {
public:
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
TreeNode* root = nullptr;
constructMaximumBinaryTree(root, nums.begin(), nums.end());
return root;
}
private:
static void constructMaximumBinaryTree(TreeNode*& root,
vector<int>::iterator start,
vector<int>::iterator end) {
if (start == end) {
return;
}
vector<int>::iterator maxIter = max_element(start, end);
root = new TreeNode(*maxIter);
constructMaximumBinaryTree(root->left, start, maxIter);
constructMaximumBinaryTree(root->right, next(maxIter, 1), end);
}
};