Description https://leetcode.com/problems/convert-bst-to-greater-tree/description/
Solution:
Recursion. Use inverse in-order recursive traversal and use a variable to store the sum greater than the current node.
class Solution {
private:
int gSum = 0;
public:
void invInOrder(TreeNode* node){
if(!node) return;
if(node->right) invInOrder(node->right);
gSum+=node->val;
node->val = gSum;
if(node->left) invInOrder(node->left);
}
TreeNode* convertBST(TreeNode* root) {
invInOrder(root);
return root;
}
};
No comments:
Post a Comment