538. Convert BST to Greater Tree

从大往小遍历,并把比当前大的值都累加在当前节点的值上面

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);
}

你可能感兴趣的:(538. Convert BST to Greater Tree)