Binary Search Tree to Greater Sum Tree

第45天。

今天的题目是Binary Search Tree to Greater Sum Tree:

感觉这道题的题意很奇怪,不清不楚的,不过看Example还是看的出他问的是什么的,挺简单的题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
TreeNode* bstToGst(TreeNode* root) {
if (root == nullptr) return root;
int sum = 0;
return bstToGst(root, sum);
}

TreeNode* bstToGst(TreeNode* root, int &sum) {
if (root == nullptr) return root;
// TreeNode *node = new TreeNode(root->val);
root->right = bstToGst(root->right, sum);
root->val = sum = root->val + sum;
root->left = bstToGst(root->left, sum);
return root;
}