Skip to content

Commit c5f73ba

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update 450. Delete Node in a BST.py
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent ee7beaf commit c5f73ba

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
9+
if root is None:
10+
return None
11+
if root.val > key:
12+
root.left = self.deleteNode(root.left, key)
13+
return root
14+
if root.val < key:
15+
root.right = self.deleteNode(root.right, key)
16+
return root
17+
if root.left is None:
18+
return root.right
19+
if root.right is None:
20+
return root.left
21+
node = root.right
22+
while node.left:
23+
node = node.left
24+
node.left = root.left
25+
root = root.right
26+
return root

0 commit comments

Comments
 (0)