Re: [閒聊] 每日leetcode

作者: nh60211as   2024-08-12 20:07:32
懶得寫今天的,挑一個 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);
}
};
作者: Smallsh (Smallsh)   2023-08-12 20:07:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com