Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
struct TreeNode* addBST(struct TreeNode* root,int & value) {
if(root == NULL){
return NULL;
}
addBST(root->right,value);
root->val += value;
value = root->val;
addBST(root->left,value);
return root;
}
TreeNode* convertBST(TreeNode* root) {
int value = 0;
if(root == NULL){
return NULL;
}
return addBST(root,value);
}
};