SDE Path

Delete a Node from a BST

Medium

Delete a Node from a BST

Delete a given value from a binary search tree while maintaining the BST property.

Input format

The first line contains level-order tokens. The second line contains X (present in the tree).

Output format

Delete X using the inorder-successor rule (replace with min of right subtree). Print preorder of result, or -1 if empty.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
93 85 N 70 86 66 N N N 1 N N 23
70
Expected output
93 85 66 1 23 86
Example 2
Input
77 36
77
Expected output
36