AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: lcg — May 11, 2008
    not yet rated
      + -

    struct node
    {
    node(int i, node * l =NULL, node * r=NULL): id(i), left(l), right(r) {};
    int id;
    node * left;
    node * right;
    };

    bool FindNode(node * root, int id, std::vector& vec)
    {
    if (root == NULL)
    return false;
    else if(root->id == id)
    {
    vec.push_back(id);
    return true;
    }
    else if ( FindNode( root->left, id, vec))
    {
    vec.push_back(root->id);
    return true;
    }
    else if ( FindNode(root->right, id, vec))
    {
    vec.push_back(root->id);
    return true;
    }
    else
    return false;

    };

    bool FindCommonAncestor( node * root, int n1, int n2, int & anc)
    {
    std::vector v1, v2;
    if (FindNode(root, n1, v1) && FindNode(root, n2, v2))
    {
    //has common;

    int anc;

    while (!v1.empty() && !v2.empty())
    {
    int a1 = v1.back();
    int a2 = v2.back();
    if ( a1 == a2)
    anc = a1;

    v1.pop_back();
    v2.pop_back();
    }

    return true;

    }
    else
    return false;
    }

  2. Submitted By: denisor — May 13, 2008
    not yet rated
      + -

    Hi, sorry for my English, I’m from Russia. My first solution was the same. But then, I wondered if tree is ordered by Id. Then algorithm could be more optimal. And your algorithm has complexity O(2N), I think one tree traversal (O(N) is enough.

  3. Submitted By: lcg — May 23, 2008
    not yet rated
      + -

    Here is a better one:

    struct node
    {
    node(int i, node * l =NULL, node * r=NULL): id(i), left(l), right(r) {};
    int id;
    node * left;
    node * right;
    };

    enum res { none = 0, first, second, both };

    res FindCommonRoot( node * root, int d1, int d2, int &anc, bool & find )
    {
    if (root == NULL)
    return none;
    else
    {
    res ret = none;
    if (root->id == d1 )
    ret = first;
    if (root->id == d2)
    ret = second;

    res r1= FindCommonRoot(root->left, d1, d2, anc, find);
    res r2 = FindCommonRoot(root->right, d1, d2, anc, find);

    ret = (res) (r1 | r2 | ret);
    if (ret == both )
    {
    anc = root->id;
    find = true;
    return none;
    }

    return ret;

    }
    }

  4. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.