这题特别好,和递归删除链表里的元素有异曲同工之妙
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* leftleave(struct TreeNode* root){
root=root->left;
while(root && root->right) root=root->right;
return root;
}
struct TreeNode* deleteNode(struct TreeNode* root, int key){
if(!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{
if(!root->left&&!root->right){
free(root);
return NULL;
}
if(root->left){
struct TreeNode* temp=leftleave(root);
root->val=temp->val;
root->left=deleteNode(root->left,temp->val);
}else{
struct TreeNode* temp=root;
root=root->right;
free(temp);
}
}
return root;
}
结果:
标签:right,TreeNode,struct,val,450,树中,二叉,root,left From: https://www.cnblogs.com/llllmz/p/18058692