| node * deleteNode(node *root, int x){ | |
| if(root == NULL) | |
| return root; | |
| if(root->data > x){ | |
| root->left = deleteNode(root->left, x); | |
| } | |
| else if(root->data < x){ | |
| root->right = deleteNode(root->right, x); | |
| } | |
| else{ | |
| if(root->left == NULL){ | |
| return root->right; | |
| }else if (root->right == NULL){ | |
| return root->left; | |
| }else{ | |
| node *temp = root->right; | |
| while(temp->left != NULL){ | |
| temp = temp->left; | |
| } | |
| int newRoot = temp->data; | |
| root->data = newRoot; | |
| root->right = deleteNode(root->right, temp->data); | |
| } | |
| } | |
| return root; | |
| } |
No comments:
Post a Comment