Skip to content

Commit 80d51cb

Browse files
committed
Commit
1 parent 9c34b05 commit 80d51cb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
13+
int sum = 0;
14+
15+
void helper(TreeNode* root){
16+
if(!root)
17+
return;
18+
19+
helper(root->right);
20+
root->val = root->val + sum;
21+
sum = root->val;
22+
helper(root->left);
23+
}
24+
25+
TreeNode* bstToGst(TreeNode* root) {
26+
helper(root);
27+
return root;
28+
}
29+
};

0 commit comments

Comments
 (0)