Open
Description
太久没做题了 没手感居然写了半小时
/**
* 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 prev = 0;
TreeNode* bstToGst(TreeNode* cur) {
if(cur == nullptr) return nullptr;
auto root = new TreeNode(0);
root->right = bstToGst(cur->right);
root->val = cur->val + prev;
prev = root->val;
root->left = bstToGst(cur->left);
return root;;
}
};