作者oin1104 (是oin的說)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Tue Jun 25 10:39:17 2024
題目 :
把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;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.142.103 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1719283159.A.6DD.html
推 CanIndulgeMe: 技術大神 06/25 10:41
推 deatheo: 大神 06/25 10:43
推 JIWP: 這題easy吧 06/25 10:48
推 smart0eddie: 大師 06/25 10:49
→ DJYOMIYAHINA: 你卷死我了 06/25 10:51
推 Furina: 我好崇拜你 06/25 11:05