Re: [閒聊] 每日leetcode

作者: oin1104 (是oin的說)   2024-06-25 10:39:17
題目 :
把binary search tree
變成Greater Sum Tree
反正就是在二元搜尋樹
對每個數字 把比他大的東西加起來
思路 :
好麻煩喔
重點就是你怎麼遍歷這棵樹
反正就是先往右找到底
然後按照順序找 先右再左
這樣一定會是從大到小
然後把數字丟進全域變數
再弄回去
就寫完了
可以打手槍了
好想打手槍
```cpp
class Solution {
public:
int all ;
void go(TreeNode* root)
{
if(root==NULL)return;
go(root->right);
all += root->val;
root->val = all;
go(root->left);
return;
}
TreeNode* bstToGst(TreeNode* root)
{
all = 0;
go(root);
return root;
}
};
```
作者: CanIndulgeMe (CIM)   2024-06-25 10:41:00
技術大神
作者: deatheo (逆十字)   2024-06-25 10:43:00
大神
作者: JIWP (JIWP)   2024-06-25 10:48:00
這題easy吧
作者: smart0eddie (smart0eddie)   2024-06-25 10:49:00
大師
作者: DJYOMIYAHINA (通通打死)   2024-06-25 10:51:00
你卷死我了
作者: Furina (芙寧娜)   2024-06-25 11:05:00
我好崇拜你

Links booklink

Contact Us: admin [ a t ] ucptt.com