卖萌的弱渣

I am stupid, I am hungry.

Remove Node in Binary Search Tree

Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.

Example

Given binary search tree:

1
2
3
4
5
    5
   / \
  3   6
 / \
2   4

Remove 3, you can either return:

1
2
3
4
5
    5
   / \
  2   6
   \
4

or

1
2
3
4
5
    5
   / \
  4   6
 /
2

Solution

(Remove-Node-in-BST.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""
class Solution:
    """
    @param root: The root of the binary search tree.
    @param value: Remove the node with given value.
    @return: The root of the binary search tree after removal.
    """
    def findNode(self, root, value):
        if root == None:
            return None

        if root.val == value:
            return root
        if root.val > value:
            return self.findNode(root.left, value)
        else:
            return self.findNode(root.right, value)

    def deleteNoLeftNode(self, root,node):
        if root.left == node:
            root.left = node.right
            return
        if root.right == node:
            root.right = node.right
            return

        if root.val > node.val:
            self.deleteNoLeftNode(root.left,node)
        else:
            self.deleteNoLeftNode(root.right,node)
        return

    def findLargestChild(self,node):
        while node.left != None:
            node = node.left
        while node.right != None:
            node = node.right
        return node

    def replaceNode(self,root, newNode, oldNode):
        if root.left == oldNode:
            root.left = newNode
            return
        if root.right == oldNode:
            root.right = newNode
            return
        if root.val > oldNode.val:
            self.replaceNode(root.left,newNode, oldNode)
        else:
            self.replaceNode(root.right,newNode,oldNode)
        return

    def removeNode(self, root, value):
        # write your code here
        if root == None:
            return None
        # find the target
        node = self.findNode(root,value)
        if node == None:
            return root

        # no left child, just delete or replace by the right child
        if node.left == None:
            if root.val == value:
                return None
            self.deleteNoLeftNode(root,node)
            return root
        # has left child, need to find the most right one to replace
        LargestChild = self.findLargestChild(node)
        # delete largestchild
        self.deleteNoLeftNode(root,LargestChild)
        LargestChild.left = node.left
        LargestChild.right = node.right
        # replace node with largestchild
        if root.val == value:
            return LargestChild

        self.replaceNode(root,LargestChild,node)

        return root