/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* leftleave(struct TreeNode* root){
if(root->left){
root=root->left;
while(root->right) root=root->right;
}else{
root=NULL;
}
return root;
}
struct TreeNode* deleteNode(struct TreeNode* root, int key){
if(!root) return NULL;
if(!root->left && !root->right && root->val==key ) {
free(root);
return NULL;
}
if(root->val > key){
root->left=deleteNode(root->left,key);
}else if(root->val <key){
root->right=deleteNode(root->right,key);
}else{
struct TreeNode* temp=leftleave(root);
if(temp){
root->val=temp->val;
root->left=deleteNode(root->left,root->val);
}else{
temp=root->right;
free(root);
return deleteNode(temp,key);
}
}
return root;
}
标签:right,TreeNode,struct,val,450,树中,二叉,root,left From: https://www.cnblogs.com/llllmz/p/18087804