从大往小遍历,并把比当前大的值都累加在当前节点的值上面
struct TreeNode* RML(struct TreeNode* root, int * add) {
if(root){
RML(root->right, add);
root->val += *add;
*add = root->val;
RML(root->left, add);
}
return root;
}
struct TreeNode* convertBST(struct TreeNode* root) {
int add = 0;
return RML(root, &add);
}
网友评论