knakul853

Untitled

Jun 10th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     TreeNode* deleteNode(TreeNode* root, int key) {
  7.        
  8.         if( !root ) return NULL;
  9.        
  10.        
  11.            if(root->val > key){
  12.               root->left =  deleteNode(root->left, key);
  13.                return root;
  14.            }
  15.           else  if(root->val < key){
  16.               root->right =  deleteNode(root->right, key);
  17.               return root;
  18.            }
  19.        
  20.         else
  21.         {
  22.            
  23.             auto node = findLeft(root->right);
  24.             if(node ==NULL) return root->left;
  25.             node->left = root->left;
  26.             return root->right;
  27.         }
  28.        
  29.         return root;
  30.        
  31.     }
  32.    
  33.     TreeNode* findLeft(TreeNode* root)
  34.     {
  35.         while(root != NULL &&root->left != NULL )root=root->left;
  36.        
  37.         return root;
  38.        
  39.        
  40.     }
  41. };
Add Comment
Please, Sign In to add comment