538. Convert BST to Greater Tree
[思路]
该节点的值 = 节点旧值 + 大于此节点的值的和;
bst树有序的;
顶点和右边的节点:那么节点的值 = 节点旧值+ 节点右值的和;
左边的节点: 节点旧值+ 顶点的值;
所有从右向左遍历;
TreeNode* convertBST(TreeNode* root) {
//找到所有的值;
int sum =0;
searchTree(root,sum);
return root;
}
void searchTree(TreeNode* root, int& sum){
if(! root)return;
if(root->right)searchTree(root->right,sum);
root->val +=sum;
sum = root->val;
if(root->left)searchTree(root->left,sum);
}