# LeetCode notes: 538. Convert BST to Greater Tree

## Question (Easy):

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. Example: Input: The root of a Binary Search Tree like this: Output: The root of a Greater Tree like this: ## General idea:

A binary search tree (BST) is given and transformed into a larger tree, that is, each node value in the original BST is transformed into the sum of its value plus all node values larger than it in the tree. example: Enter: binary search tree as follows: The output is as follows: ## Idea:

The meaning of the topic is that each value in the node adds up all the values larger than it in the tree and adds itself, which is its new value.

This must traverse the whole tree to know which numbers are larger than a node value. Because the position of the node cannot be changed, it can only be operated directly on the tree. Here we need to use the nature of binary search tree: large on the right and small on the left.

We use traversal.

For each node, nodes larger than it are: parent node, all nodes in the parent node's right child node tree, its own right child node, and all nodes in its own right child node tree. Therefore, when calculating the new value of a node, we need to add all these values as our new values.

For the left child node of a node, we need to give the new value calculated above to the left child node, which is the "sum of the values of all nodes in the parent node and the right child node tree of the parent node" of the left child node.

For the right child node of a node, we need to give the "sum of all node values on the parent node and the right child node tree of the parent node" to the right child node, because for the right child node, the parent node of its parent node and the right child tree of that node must be larger than it. The value given here is actually different from that given by the left child node. It needs to be calculated and supplied separately.

In this way, we can operate on each node and form a recursive operation.

## Code (C + +):

```/**
* 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:
int sumValue(TreeNode* root, int toLeft) {
int sum = root->val;
root->val += toLeft;
if (root->right){
int rightValue = sumValue(root->right, toLeft);
root->val += rightValue;
sum += rightValue;
}
if (root->left) {
int leftValue = sumValue(root->left, root->val);
sum += leftValue;
}
return sum;
}

TreeNode* convertBST(TreeNode* root) {
if (!root) return root;
int temp = sumValue(root, 0);
return root;
}
};```

## outside the box:

In fact, the above idea can be simplified. Start with the largest node, that is, the rightmost leaf node, go back, and save a sum with a global variable. The code is much simpler from the right child node to the node itself, and then to the left child node, but the logic needs to be clear.

```/**
* 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 {
private:
int cur_sum = 0;
public:
void travel(TreeNode* root){
if (!root) return;
if (root->right) travel(root->right);

root->val = (cur_sum += root->val);
if (root->left) travel(root->left);
}
TreeNode* convertBST(TreeNode* root) {
travel(root);
return root;
}
};```

Collection: https://github.com/Cloudox/LeetCode-Record

Posted on Tue, 23 Nov 2021 04:41:37 -0500 by phyzar