SDE Path

Inorder Successor and Predecessor in a BST

Medium

Inorder Successor and Predecessor in a BST

Given a node in a BST, find its inorder successor and predecessor.

Input format

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

Output format

Print "pred succ" where pred is the inorder predecessor and succ is the inorder successor (-1 if missing).

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
80 62 99 38 N N N 35 53 21
80
Expected output
62 99
Example 2
Input
12 5 83 N 10 57 84 N N 13 81 N N N 44 79 N 33 47 70 N 22 34
10
Expected output
5 12