1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
void del(TreeNode* root){ if (root== nullptr){ return; } del(root->left); del(root->right); free(root); } void searchDel(TreeNode *root,int x){ if(root== nullptr){ return; } if (root->val==x){
del(root); return; }
queue<TreeNode*> queue1; queue1.push(root); while (!queue1.empty()){ TreeNode* cur=queue1.front();
queue1.pop(); if (cur->left){ if (cur->left->val==x){ del(cur->left); cur->left= nullptr; } else{
queue1.push(cur->left); } } if (cur->right){ if (cur->right->val==x){ del(cur->right); cur->right= nullptr; } else{
queue1.push(cur->right); } } } }
|