迅雷的笔试可真让人煎熬啊,题量很大,而且考试时间是三个小时。不过迅雷的题目质量很高,既考查了基础知识又不乏高难的数据结构和算法题目。下面和大家分享两道算法编程题,代码没有在编译器上调试,可能会出现一点小bug,感兴趣的朋友可以编译调试一下。

题目1:将一个单链表逆转——原来的头指针变为尾指针,原来的尾指针变为头指针。


算法思想:从链表的头结点开始依次逆转,最终将整个链表逆转。

程序代码:

view plaincopy to clipboardprint?
/*节点的类定义*/ 
class Node  
{  
    Node * next;  
};  
 
/*链表的类定义*/ 
class List  
{  
    Node * head;  
};  
 
/*逆转函数——将一个单链表逆转*/ 
void ReverseList(List & list)  
{  
    Node * pre=NULL;  
    Node * cur=list.head;  
    Node * back=cur->next;  
 
    while(back!=NULL)  
    {  
        cur->next=pre;  
        pre=cur;  
        cur=back;  
        back=cur->next;  
    }  
 

/*节点的类定义*/
class Node
{
 Node * next;
};

/*链表的类定义*/
class List
{
 Node * head;
};

/*逆转函数——将一个单链表逆转*/
void ReverseList(List & list)
{
 Node * pre=NULL;
 Node * cur=list.head;
 Node * back=cur->next;

 while(back!=NULL)
 {
  cur->next=pre;
  pre=cur;
  cur=back;
  back=cur->next;
 }

}

题目2:要求寻找二叉树中两个节点的最近的公共祖先,并将其返回。view plaincopy to clipboardprint?
class Node  
{  
   Node * left;  
   Node * right;  
   Node * parent;  
};  
/*查找p,q的最近公共祖先并将其返回。*/ 
Node * NearestCommonAncestor(Node * p,Node * q); 
class Node
{
   Node * left;
   Node * right;
   Node * parent;
};
/*查找p,q的最近公共祖先并将其返回。*/
Node * NearestCommonAncestor(Node * p,Node * q);

算法思想:这道题的关键在于每个节点中包含指向父节点的指针,这使得程序可以用一个简单的算法实现。首先给出p的父节点p->parent,然后将q的所有父节点依次和p->parent作比较,如果发现两个节点相等,则该节点就是最近公共祖先,直接将其返回。如果没找到相等节点,则将q的所有父节点依次和p->parent->parent作比较......直到p->parent==root。

 

程序代码:

view plaincopy to clipboardprint?
Node * NearestCommonAncestor(Node * root,Node * p,Node * q)  
{  
    Node * temp;  
         while(p!=NULL)  
    {  
        p=p->parent;  
        temp=q;  
        while(temp!=NULL)  
        {  
            if(p==temp->parent)  
                return p;  
            temp=temp->parent;  
        }  
    }  

Node * NearestCommonAncestor(Node * root,Node * p,Node * q)
{
 Node * temp;
         while(p!=NULL)
 {
  p=p->parent;
  temp=q;
  while(temp!=NULL)
  {
   if(p==temp->parent)
    return p;
   temp=temp->parent;
  }
 }
}


知识拓展:对于第二个二叉树的问题,如果节点中不包含指向父节点的指针应该怎么计算?


算法思想:如果一个节点的左子树包含p,q中的一个节点,右子树包含另一个,则这个节点就是p,q的最近公共祖先。


程序代码:

view plaincopy to clipboardprint?
/*查找a,b的最近公共祖先,root为根节点,out为最近公共祖先的指针地址*/ 
int FindNCA(Node* root, Node* a, Node* b, Node** out)   
{   
    if( root == null )   
    {   
        return 0;   
    }  
 
    if( root == a || root == b )  
    {      
        return 1;  
    }  
 
    int iLeft = FindNCA(root->left, a, b, out);  
    if( iLeft == 2 )  
    {      
        return 2;  
    }  
 
    int iRight = FindNCA(root->right, a, b, out);  
    if( iRight == 2 )  
    {      
        return 2;  
    }  
 
    if( iLeft + iRight == 2 )  
    {     
        *out == root;  
    }  
    return iLeft + iRight;  
}  
 
void main()   
{   
    Node* root = ...;   
    Node* a = ...;   
    Node* b = ...;   
    Node* out = null;   
    int i = FindNCA(root, a, b, &out);   
    if( i == 2 )   
    {   
        printf("Result pointer is %p", out);   
    }   
    else   
    {   
        printf("Not find pointer");   
    }   

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/piaojun_pj/archive/2010/10/28/5971125.aspx

posted on 2011-02-10 14:36  higirle  阅读(852)  评论(0编辑  收藏  举报