Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Get N biggest nodes from an AVL tree

Given the follow AVL tree:

         2007
       /     \
      /       \
    2005       2010
    /   \         \
   /     \         \
 2003    2006     2012

How can I get the N biggest nodes?

Example input: 4

Example output: {2012, 2010, 2007, 2006}

I only managed to get the biggest node by doing this:

node* findMax(node* t)
    {
        if(t == NULL)
            return NULL;
        else if(t->right == NULL)
            return t;
        else
            return findMax(t->right);
}

But I'm unable to get the N biggest nodes.

Cheers!

Comments