-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path450.Delete-Node-in-a-BST.py
58 lines (50 loc) · 1.38 KB
/
450.Delete-Node-in-a-BST.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
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
# https://leetcode.com/problems/delete-node-in-a-bst/
#
# algorithms
# Medium (38.78%)
# Total Accepted: 52,907
# Total Submissions: 136,417
# beats 93.27% of python submissions
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def deleteNode(self, root, key):
"""
:type root: TreeNode
:type key: int
:rtype: TreeNode
"""
node = root
parent = root
while node and node.val != key:
parent = node
if node.val > key:
node = node.left
else:
node = node.right
if not node:
return root
def delete_node(node):
if not node.left and not node.right:
return None
if not node.left:
return node.right
if not node.right:
return node.left
tmp = node.right
while tmp.left:
tmp = tmp.left
tmp.left = node.left
return node.right
new_node = delete_node(node)
if parent.val > key:
parent.left = new_node
return root
if parent.val < key:
parent.right = new_node
return root
return new_node