category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Delete Node in a BST

 TreeNode* dfs(TreeNode* root, int key) {
        if (root == nullptr) {
            return root;
        }
        if (root->val == key) {
            if (root->right) {
                
                auto temp = root->right;
                while (temp->left != nullptr) temp = temp->left;
               temp->left = root->left;
                
                return root->right;
            }
            else {
                return root->left;
            }
            
        }
        
        root->left = dfs(root->left, key); 
        root->right = dfs(root->right, key); 
        return root;
    }
    
    TreeNode* deleteNode(TreeNode* root, int key) {
        root = dfs(root, key); 
        return root;
        
    }