Skip to content

Commit 08a3a37

Browse files
authored
Create 1038. Binary Search Tree to Greater Sum Tree (#512)
2 parents afb2553 + 06e8b38 commit 08a3a37

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
private:
3+
int sum = 0;
4+
5+
void traverse(TreeNode* root) {
6+
if (root) {
7+
traverse(root->right); // Traverse the right subtree
8+
sum += root->val; // Update the sum
9+
root->val = sum; // Update the current node's value
10+
traverse(root->left); // Traverse the left subtree
11+
}
12+
}
13+
14+
public:
15+
TreeNode* bstToGst(TreeNode* root) {
16+
traverse(root);
17+
return root;
18+
}
19+
};

0 commit comments

Comments
 (0)