Re: [閒聊] 每日leetcode
1261.
第一次用lambda接出來遞回
有種很壞壞的感覺
好爽ㄛ
其實陣列就是整棵樹了
很想把原本的樹都蛋雕==
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class FindElements {
public:
bitset<1000001> check;
FindElements(TreeNode* root) {
root->val = 0;
TreeNode* nd = root;
function< void (TreeNode*) > f ;
f = [&](TreeNode* nd){
int x = nd->val;
if(x > 1000000) return;
check[x] = 1;
if(nd->left){
nd->left->val = 2 * x + 1;
f(nd->left);
}
if(nd->right){
nd->right->val = 2 * x + 2;
f(nd->right);
}
};
f(nd);
}
bool find(int target) {
return check[target];
}
};
/**
* Your FindElements object will be instantiated and called as such:
* FindElements* obj = new FindElements(root);
* bool param_1 = obj->find(target);
*/
※ 引述《oin1104 (是oin的說)》之銘言:
: 題目
: 給你一顆樹
: 每個節點的數字是父節點的2*x+1
: 找出裡面會不會出現特定數字
: 思路
: 遞迴一次樹
: 把出現的數字存起來
: 找的時候直接找就好
--
很姆的咪
姆之咪
http://i.imgur.com/5sw7QOj.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.15.22.90 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1740165881.A.F92.html
推
02/22 03:30,
9月前
, 1F
02/22 03:30, 1F
推
02/22 03:30,
9月前
, 2F
02/22 03:30, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 1342 之 1552 篇):