/**
* 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&&root->right) root=root->right;
return root;
}
return NULL;
}
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,temp->val);
}else{
temp=root->right;
free(root);
return temp;
}
}
return root;
}
结果:
标签:right,TreeNode,struct,450,树中,二叉,return,root,left From: https://www.cnblogs.com/llllmz/p/18073760