Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- knakul853
- */
- class Solution {
- public:
- TreeNode* deleteNode(TreeNode* root, int key) {
- if( !root ) return NULL;
- if(root->val > key){
- root->left = deleteNode(root->left, key);
- return root;
- }
- else if(root->val < key){
- root->right = deleteNode(root->right, key);
- return root;
- }
- else
- {
- auto node = findLeft(root->right);
- if(node ==NULL) return root->left;
- node->left = root->left;
- return root->right;
- }
- return root;
- }
- TreeNode* findLeft(TreeNode* root)
- {
- while(root != NULL &&root->left != NULL )root=root->left;
- return root;
- }
- };
Add Comment
Please, Sign In to add comment