SDE Path

Find the Kth Smallest Element in a BST

Medium

Find the Kth Smallest Element in a BST

Find the Kth smallest value in a binary search tree using inorder traversal.

Input format

The first line contains level-order tokens. The second line contains K (1 ≤ K ≤ tree size).

Output format

Print the Kth smallest value in the BST.

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
79
1
Expected output
79
Example 2
Input
69 68 98 26 N 95 N N 55 N N 50 62 38
2
Expected output
38