题目描述:
https://leetcode-cn.com/problems/convert-bst-to-greater-tree/
解题思路:
右-中-左访问节点,将值累加即可;
代码:
class Solution {
public:
TreeNode* convertBST(TreeNode* root) {
if(!root)
return NULL;
int sum=0;
back(root, sum);
return root;
}
void back(TreeNode* root, int& sum)
{
if(!root)
return;
back(root->right, sum);
root->val+=sum;
sum = root->val;
back(root->left, sum);
}
};
网友评论