Leetcode 1114 Solution
This article provides solution to leetcode question 1114 (binary-search-tree-to-greater-sum-tree)
Access this page by simply typing in "lcs 1114" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree
Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
int s;
public:
void doConvertBST(TreeNode* root) {
if (root == NULL)
return;
doConvertBST(root->right);
s += root->val;
root->val = s;
doConvertBST(root->left);
}
TreeNode* bstToGst(TreeNode* root) {
s = 0;
doConvertBST(root);
return root;
}
};