JustPaste.it

Q1 : Given a binary search tree. Write an iterator which returns the elements in ascending order.

 

    BSTIterator{
        
        public BSTIterator(Node root) {
            //implement method
        }

        int getNext(){
            //return next node in ascending order
            //implement method
        }

        bool hasNext() {
            //wether there is a next node or not
            //implement method
        }
    }
    
 
Q2 : There is a triangle ABC. You can move from 1 vertex to another in one step. 
    If you start from vertex A, find the number of ways to return to vertex A after exactly n steps.

    Example :

    n = 0    , 1 possible way
    n = 1    , no possible way
    n = 2    , 2 possible ways (ABA, ACA)
    n = 3    , 2 possible ways (ABCA, ACBA)
    n = 4     , 6 possible ways (ABABA, ABCBA, ABACA, ACACA, ACBCA, ACABA)