Skip to content

Commit 0aa0f3d

Browse files
Added solution
1 parent f6046d5 commit 0aa0f3d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

bst_to_greater_tree.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
TreeNode* convertBST(TreeNode* root) {
15+
if(root){
16+
convertBST(root->right);
17+
root->val += sum;
18+
sum = root->val;
19+
convertBST(root->left);
20+
}
21+
return root;
22+
}
23+
};

0 commit comments

Comments
 (0)