Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
        Implementation
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms
      • 10
        Basic Dynamic Programming
      • 11
        Recursion
      • 12
        Linked LIst
      • 13
        Queue & Stack
      • 14
        Binary tree + BST
      • 15
        Divide & Conquer + Advanced Sorting
      • 16
        Heap
      • 17
        Hashing
      • 18
        Graph Representation
      • 19
        BFS

  • Find the k-th smallest value in a BST

    Given an empty binary search tree with no nodes, you are asked to perform 2 kinds of queries:
    1. insert x - insert the value x in the BST
    1. smallest k - print the kth smallest element in the BST
    Given q queries, you are asked to write a program to perform these queries.

    Input

    The first line of the input contains a single number q (1 ≀ q ≀ 1000).
    The next q lines contain queries. For all the insert queries, the value of x does not exceed in absolute value. For all the smallest queries, the value of k does not exceed the current tree size.

    Output

    For each smallest query, the program should print the smallest kth element in the binary search tree.

    Examples

    Input
    Output
    8 insert 2 insert 1 smallest 1 smallest 2 insert 10 insert 20 smallest 3 smallest 2
    1 2 10 2
    Β 

    Constraints

    Time limit: 1 seconds

    Memory limit: 512 MB

    Output limit: 1 MB

    To check your solution you need to sign in
    Sign in to continue