← 返回 bytedance 的题目列表BST Node Search and Delete
类型:online_judge
Given the root of a Binary Search Tree (BST) and an integer value, write functions to search the node with this value, and then to delete this node. You need to implement your own test cases and the tree class.
Definition of a tree node is given as:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
Function signatures provided:
def searchBST(root: TreeNode, val: int) -> TreeNode:
# Implement the search logic here
pass
def deleteNode(root: TreeNode, key: int) -> TreeNode:
# Implement the delete logic here
pass
Input: The root of the BST, and the integer value to search or delete.
Output: The search returns the target node, and delete returns the root of the modified tree.
Example:
Input:
Search: [4, 2, 7, 1, 3], 2
Delete: [5, 3, 6, 2, 4, null, 7], 3
Output:
Search: Node 2
Delete: [5, 4, 6, 2, null, null, 7]
Constraints: Number of nodes in the range [1, 10000].
Example
Input
searchBST [4, 2, 7, 1, 3] 2