0%
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: Optional[TreeNode], key: int) -> Optional[TreeNode]: if root==None: return None if root.val==key: if root.left==None and root.right==None: return None elif root.left==None and root.right: return root.right elif root.left and root.right==None: return root.left else: cur = root.left while cur.right: cur = cur.right cur.right = root.right return root.left if root.val > key: root.left = self.deleteNode(root.left,key) elif root.val < key: root.right = self.deleteNode(root.right,key) return root
|