1. 程式人生 > >450. Delete Node in a BST(python+cpp)

450. Delete Node in a BST(python+cpp)

題目:

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 node is found, delete the node.
Note: Time complexity should be O(height of tree).

Example:

root = [5,3,6,2,4,null,7]
key = 3

     5    
    / \   
   3   6  
  / \   \ 
 2   4   7

Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the following BST.

     5    
    / \   
   4   6  
  /     \ 
 2       7

Another valid answer is [5,2,6,null,4,null,7].

    5    
   / \   
  2   6    
   \   \
    4   7

解釋:
刪除BST中的一個結點,使得刪除後樹還是一個BST
有多種刪除方法
遞迴,如果val小於當前結點的val,在其左子樹中找,反之亦然~
1.結點沒有左子樹:返回其右子樹
2.結點沒有右子樹:返回其左子樹
3.結點既有左子樹,又有右子樹:
1)在右子樹中找一個值最小的結點,替換被刪除的結點,並刪除找到的最小結點(左孩子的左孩子的左孩子……)

    root.val = minNode.val;
    root.right = deleteNode(root.right, root.val);

或者
2)在左子樹中找到一個值最大的結點,替換被刪除的結點,並刪除找到的最大結點(右孩子的右孩子的右孩子……)
python程式碼:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def deleteNode(self, root, key):
        """
        :type root: TreeNode
        :type key: int
        :rtype: TreeNode
        """
        def findMax(root):
            if not root:
                return None
            while root.right:
                root=root.right
            return root
        def findMin(root):
            if not root:
                return None
            while root.left:
                root=root.left
            return root
        if not root:
            return None
        if root.val>key:
            root.left=self.deleteNode(root.left,key)
        elif root.val<key:
            root.right=self.deleteNode(root.right,key)
        else:
            if not root.left:
                return root.right
            elif not root.right:
                return root.left
            else:
                # minNode=findMin(root.right)
                # root.val=minNode.val
                # root.right=self.deleteNode(root.right,root.val)
                maxNode=findMax(root.left)
                root.val=maxNode.val
                root.left=self.deleteNode(root.left,root.val)
        return root

c++程式碼:

/**
 * 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)
            return NULL;
        if(key>root->val)
            root->right=deleteNode(root->right,key);
        else if(key<root->val)
            root->left=deleteNode(root->left,key);
        else
        {
            if(!root->left)
                return root->right;
            else if(!root->right)
                return root->left;
            else
            {
                TreeNode* maxNode=findMax(root->left);
                root->val=maxNode->val;
                root->left=deleteNode(root->left,maxNode->val);
            }
        }
        return root;
    }
    TreeNode* findMax(TreeNode* root)
    {
        if (!root)
            return NULL;
        while(root->right)
            root=root->right;
        return root;
    }
};

總結: