forked from gh877916059/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path087. 删除二叉查找树的节点.cpp
36 lines (36 loc) · 940 Bytes
/
087. 删除二叉查找树的节点.cpp
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
class Solution
{
public:
TreeNode* removeNode(TreeNode* root, int value)
{
if(root==NULL)
return NULL;
if(value<root->val)
root->left = removeNode(root->left,value);
else if(value>root->val)
root->right = removeNode(root->right,value);
else
{
if(root->right==NULL&&root->left==NULL)
return NULL;
else if(root->right!=NULL&&root->left!=NULL)
{
root->val = findMin(root->right);
root->right = removeNode(root->right,root->val);
}
else
{
root = (root->left != NULL)?root->left:root->right;
}
}
return root;
}
private:
int findMin(TreeNode* root)
{
if(root->left!=NULL)
return findMin(root->left);
else
return root->val;
}
};