delete-node-in-a-bst

https://leetcode.com/problems/delete-node-in-a-bst/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (root == NULL) {
            return NULL;
        }

        TreeNode* cur = root;
        TreeNode* last = NULL;
        while (cur != NULL && cur->val != key) {
            if (cur->val > key) {
                last = cur;
                cur = cur->left;
            }
            else {
                last = cur;
                cur = cur->right;
            }
        }

        if (cur == NULL) {
            return root;
        }

        TreeNode* tlast = cur;
        TreeNode* tcur;
        if (cur->left != NULL) {
            tcur = cur->left;
            while (tcur->right != NULL) {
                tlast = tcur;
                tcur = tcur->right;
            }

            if (tcur == tlast->left) {
                tlast->left = tcur->left;
            }
            else {
                tlast->right = tcur->left;
            }
            cur->val = tcur->val;
        }
        else if (cur->right != NULL) {
            tcur = cur->right;
            while (tcur->left != NULL) {
                tlast = tcur;
                tcur = tcur->left;
            }
            if (tcur == tlast->left) {
                tlast->left = tcur->right;
            }
            else {
                tlast->right = tcur->right;
            }
            cur->val = tcur->val;
        }
        else {
            if (last == NULL) {
                // only root
                return NULL;
            }
            if (cur == last->left) {
                last->left = NULL;
            }
            else if (cur == last->right){
                last->right = NULL;
            }
            else {
                // error
            }
        }
        return root;
    }
};
时间: 2024-08-10 04:17:45

delete-node-in-a-bst的相关文章

450. Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

Leetcode: Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

450. Delete Node in a BST 删除bst中的一个节点

[抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If

[Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

LC 450. Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

leetcode_237题——Delete Node in a Linked List(链表)

Delete Node in a Linked List Total Accepted: 6113 Total Submissions: 12798My Submissions Question Solution Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2

leetCode 237. Delete Node in a Linked List 链表

237. Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked

leetcode:237 Delete Node in a Linked List-每日编程第四题

Delete Node in a Linked List Total Accepted: 47385 Total Submissions: 107608 Difficulty: Easy Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 ->

237. Delete Node in a Linked List(C++)

237. Delete Node in a Linked Lis t Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked

[LeetCode][JavaScript]Delete Node in a Linked List

Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list