-
Notifications
You must be signed in to change notification settings - Fork 17
/
450-delete-node-in-a-bst.py
31 lines (28 loc) · 1023 Bytes
/
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
def delete_helper(node):
if not node:
return node
if node.val == key:
if not node.left:
return node.right
pred = node.left
while pred and pred.right:
pred = pred.right
pred.right = node.right
return node.left
elif node.val < key:
node.right = delete_helper(node.right)
else:
node.left = delete_helper(node.left)
return node
return delete_helper(root)
# time O(n), due to tree height, can be logn
# space O(n), due to recursive stack
# using tree and divide and conquer and bst