Binary Search Tree to Greater Sum Tree LeetCode solution in C++

1
class Solution {
2
public:
3
int s = 0;
4

5
void solve(TreeNode *root) {
6
if (!root) return;
7
solve(root->right);
8

9
root->val = s + root->val;
10
s = root->val;
11

12
solve(root->left);
13
return;
14
}
15

16
TreeNode *bstToGst(TreeNode *root) {
17
if (!root) return NULL;
18
solve(root);
19
return root;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0