<p>Given the <code>root</code> of 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 the sum of all keys greater than the original key in BST.</p>

<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints:</p>

<ul>
	<li>The left subtree of a node contains only nodes with keys <strong>less than</strong> the node&#39;s key.</li>
	<li>The right subtree of a node contains only nodes with keys <strong>greater than</strong> the node&#39;s key.</li>
	<li>Both the left and right subtrees must also be binary search trees.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/02/tree.png" style="width: 500px; height: 341px;" />
<pre>
<strong>Input:</strong> root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
<strong>Output:</strong> [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [0,null,1]
<strong>Output:</strong> [1,null,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
	<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
	<li>All the values in the tree are <strong>unique</strong>.</li>
	<li><code>root</code> is guaranteed to be a valid binary search tree.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as 1038: <a href="https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/" target="_blank">https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/</a></p>

// /**
//  * 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 {
// public:
//     // int pre=0;

//     // TreeNode* convertBST(TreeNode* root) {
//     //     if(root==nullptr) return root;
//     //     convertBST(root->right);
//     //     root->val+=pre;
//     //     pre=root->val;
//     //     convertBST(root->left);
        

//     //     return root;
//     // }

//     TreeNode* convertBST(TreeNode* root) {
//         if(root==nullptr) return root;
//         stack<TreeNode*> s;
//         TreeNode* r=root;
//         int pre=0;
//         TreeNode* t=root;
//         while(t || !s.empty())
//         {
//             if(t!=nullptr)
//             {
//                 s.push(t);
//                 t=t->right;
//             }
//             else
//             {
//                 t=s.top();
//                 s.pop();
//                 t->val+=pre;
//                 pre=t->val;
//                 t=t->left;
//             }
//         }
//         return r;
//     }
// };








/**
 * 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 {
public:
    TreeNode* convertBST(TreeNode* root) {
        return nullptr;
    }
};

int nums[10002];
string nodes[20001];
int indexes[20001];

static auto _ = []() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  return nullptr;
}();

int init = [] {
    ofstream out("user.out");
    cout.rdbuf(out.rdbuf());

    for(string s; getline(cin,s);) {
        int n{};
        int m{};
        for(int _i{1}, _n = s.size() - 1; _i < _n; ++n, ++_i) {
            if (s[_i] == 'n') {
                nodes[n] = "null";
                _i += 4;
                continue;
            }
            int sign{1};
            int v = s[_i++]&15;
            if (v > 10) {
                sign = -1;
                v = 0;
            }
            while((s[_i]&15) < 10) v = v*10 + (s[_i++]&15);
            v *= sign;
            nums[m++] = v;
            indexes[10000 + v] = n;
        }
        if (n == 0) {
            cout << "[]\n";
            continue;
        }
        if (n == 1) {
            cout << '[' << nums[0] << "]\n";
            continue;
        }
        sort(nums, nums + m);
        nums[m] = 0;
        for (int i{m - 1}; i >= 0; --i) {
            const int index = indexes[nums[i] + 10000];
            nums[i] = nums[i + 1] + nums[i];
            nodes[index] = to_string(nums[i]);
        }

        cout << '[';
        for (int i{}; i < n - 1; ++i) cout << nodes[i] << ',';
        cout << nodes[n - 1] << "]\n";
    }
    exit(0);
    return 0;
}();
11100