您的位置:首页 > 其它

笔试算法题(25):复制拥有多个指针的链表 & 判断二元树B是否为A的子树

2014-05-23 10:29 543 查看
出题:定义一个复杂链表:在单向链表的基础上,每个节点附加一个指向链表中其他任意节点的指针sibling,实现CNode* Clone(Cnode *head)函数复制这个复杂链表;

分析:

解法1:将head复制到CHead中,第一次遍历创建CHead中对应head的各个节点(next),第二次遍历创建CHead中对应head各个节 点的sibling链接,由于需要在CHead中找到对应head中的sibling节点,所以需要遍历CHead链表,但是可以用空间换时间的方法:使 用Hash Table存储CHead和head对应的节点对,这样head中定位了sibling之后,就可以知道CHead中对应sibling节点,时间复杂度 为O(N),空间复杂度为O(N);

解法2:注意给出的函数头中参数并没有使用const,而一般情况下的copy函数的参数都是const,所以可以推断可能需要改变原始数据的结构。使用 奇偶链表实现,将新创建的CHead中的各个节点对应安插到head中各个节点之后,这样CHead中与head中对等的节点就在head中节点的后面, 所以可以很容易确定sibling节点,最后仅读取偶数节点就是CHead,时间复杂度为O(N),空间复杂度为O(1)。海涛老师再次威武!此方法参考 海涛老师的博客:
http://zhedahht.blog.163.com/blog/static/254111742010819104710337/

解题:

struct CNode {
int value;
CNode *next;
CNode *sibling;
};
CNode* Clone(CNode *head) {
if(head==NULL) return NULL;

CNode *CHead=new CNode();
CHead->value=head->value;
CNode *cur, *pre=CHead, *temp=head->next, *CTemp;
/**
* 以next指针为线索创建链表节点
* */
while(temp!=NULL) {
cur=new CNode();
cur->value=temp->value;
pre->next=cur;
pre=cur;

temp=temp->next;
}
/**
* 以next指针为线索创建sibling链接
* */
temp=head;CTemp=CHead;
while(temp!=NULL) {
/**
* 将下面这句代码换成HashTable存储的head和CHead中
* 对等节点的存储对
* */
//CTemp->sibling=temp->sibling;
temp=temp->next;
CTemp=CTemp->next;
}
return CHead;
}
/**
* 在head中每个节点的后面创建一个新节点,
* 并复制之前节点的value,链接之前节点的
* next节点
* */
void CreateNewNode(CNode *head) {
CNode *index=head, *nNode, *next;
while(index!=NULL) {
nNode=new CNode();
nNode->value=index->value;
nNode->sibling=NULL;

next=index->next;
index->next=nNode;
nNode->next=next;
index=next;
}
}
/**
* 顺序遍历head中奇数索引的节点,如果其sibling非NULL,则
* 其sibling的next节点就是CHead中的对应节点的sibling
* */
void CopySibling(CNode *head) {
CNode *index=head;
while(index!=NULL) {
if(index->sibling!=NULL) {
index->next->sibling=
index->sibling->next;
}
index=index->next->next;
}
}
/**
* 将head拆分成奇数索引节点和偶数索引节点,后者就是新clone
* 的复杂链表
* */
CNode* Separate(CNode *head) {
CNode *index=head, *clone;
if(index!=NULL) {
clone=index->next;
index->next=index->next->next;
index=index->next;
} else
return NULL;
CNode *temp;
while(index!=NULL) {
temp=index->next;
clone->next=temp;
index->next=temp->next;

clone=temp;
index=temp->next;
}

return clone;
}
void Deletion(CNode *head) {
CNode *cur=head, *next;
if(cur!=NULL) {
next=cur->next;
delete cur;
cur=next;
}
}


出题:输入两棵二元树的根节点A和B,判断B是否是A的一个子树结构;

分析:

首先在A中找到与B根节点的值相同的节点AB1(注意处理节点值相同的情况,首先判断当前找到的AB1是否匹配,如果不匹配则继续寻找下一个AB1),然 后同时递归AB1和B,如果B中某节点的值与AB1某节点的值不等,则返回false;

如果B中某节点有子节点(左右),而AB1中没有,则返回 false;当B递归完全之后(也就是到达NULL)返回true;

解题:

struct Node {
int value;
Node *left;
Node *right;
};
/**
* son只能是father的一部分,其他情况都返回false
* */
bool compare(Node *father, Node *son) {
if(son==NULL) return true;
if(father==NULL) return false;
if(father->value != son->value)
return false;
else
return compare(father->left,son->left) &&
compare(father->right,son->right);
}
/**
* 注意处理节点值重复的情况,因为只能找到第一个节点,而正确
* 的匹配可能发生在之后的拥有相同节点值的节点
* */
Node* findNode(Node *root, Node *target) {
Node *temp=NULL; bool ismatch=true;
if(root->value==target->value) {
if(compare(root, target)) {
printf("\nb1 is part of a1");
exit(0);
} else
ismatch=false;
}
if(!ismatch && root->left!=NULL)
temp=findNode(root->left,target);
if(temp==NULL && root->right!=NULL)
temp=findNode(root->right,target);
return temp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐