题目:
题解:
class Solution {
public:TreeNode* getSuccessor(TreeNode* node) {TreeNode* succ = node->right;while (succ->left != nullptr && succ->left != node) {succ = succ->left;}return succ;}TreeNode* convertBST(TreeNode* root) {int sum = 0;TreeNode* node = root;while (node != nullptr) {if (node->right == nullptr) {sum += node->val;node->val = sum;node = node->left;} else {TreeNode* succ = getSuccessor(node);if (succ->left == nullptr) {succ->left = node;node = node->right;} else {succ->left = nullptr;sum += node->val;node->val = sum;node = node->left;}}}return root;}
};