
要进行转这棵树,仅仅需要进行先遍历右子树,遍历中树,遍历左子树,然后加中间就ok
/**
* 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 sum = 0;
TreeNode* convertBST(TreeNode* root) {
if(!root) return NULL;
convertBST(root->right);
root->val = root->val + sum;
sum = root->val;
convertBST(root->left);
return root;
}
};
来源:https://www.cnblogs.com/littlepage/p/12274184.html