Re: [閒聊] 每日leetcode
看板Marginalman作者nh60211as (xXx_5354M3_31M0_xXx)時間1年前 (2024/08/12 20:07)推噓1(1推 0噓 1→)留言2則, 2人參與討論串700/1554 (看更多)
懶得寫今天的,挑一個 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);
}
};
--
https://i.imgur.com/07Uv9NC.png










--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.228.71.204 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1723464456.A.4D2.html
→
08/12 20:07,
1年前
, 1F
08/12 20:07, 1F
推
08/13 15:25,
1年前
, 2F
08/13 15:25, 2F
討論串 (同標題文章)
完整討論串 (本文為第 700 之 1554 篇):