forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0450.py
29 lines (26 loc) · 827 Bytes
/
0450.py
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
class Solution:
def deleteNode(self, root, key):
"""
:type root: TreeNode
:type key: int
:rtype: TreeNode
"""
if not root:
return None
if key < root.val:
root.left = self.deleteNode(root.left, key)
return root
elif key > root.val:
root.right = self.deleteNode(root.right, key)
return root
else:
if not root.left:
return root.right
if not root.right:
return root.left
minNode = root.right
while minNode.left:
minNode = minNode.left
root.val = minNode.val
root.right = self.deleteNode(root.right, minNode.val)
return root