标签:style c class blog code java
出题:判断一个单向链表是否有环,如果有环则找到环入口节点;
分析:
示意图 |
解题:
1 struct Node { 2 int v; 3 Node *next; 4 }; 5 Node* IsCycle(Node *head) { 6 Node *fast=head, *slow=head; 7 8 while(true) { 9 if(fast!=NULL) 10 fast=fast->next; 11 if(fast!=NULL) 12 fast=fast->next; 13 else 14 return NULL; 15 if(slow!=NULL) 16 slow=slow->next; 17 else 18 return NULL; 19 20 if(fast==slow) 21 return fast; 22 } 23 } 24 Node* FindEntry(Node *head, Node *joint) { 25 Node *m=head, *n=joint; 26 while(true) { 27 if(m==n) 28 return m; 29 m=m->next; 30 n=n->next; 31 } 32 } 33 int main() { 34 Node* b1=new Node(); b1->v=1; 35 Node* b2=new Node(); b2->v=2;b1->next=b2; 36 Node* b3=new Node(); b3->v=3;b2->next=b3; 37 Node* b4=new Node(); b4->v=4;b3->next=b4; 38 Node* b5=new Node(); b5->v=5;b4->next=b5; 39 Node* b6=new Node(); b6->v=6;b5->next=b6; 40 Node* b7=new Node(); b7->v=7;b6->next=b7; 41 Node* b8=new Node(); b8->v=8;b7->next=b8; b8->next=b3; 42 43 Node* temp; 44 if((temp=IsCycle(b1))!=NULL) { 45 printf("\nthe joint point is: %d",temp->v); 46 printf("\nthe entry of cycle is: %d",FindEntry(b1,temp)->v); 47 } 48 else 49 printf("\nthere is no cycle."); 50 return 0; 51 }
出题:判断两棵二元树是否相等(左右子树不能交叉比较);
分析:使用递归实现,在树的K层,有2^K 个节点,所以会进行(2^K)*2次调用,所以时间复杂度为O(N);
解题:
1 struct Node { 2 int value; 3 Node *left; 4 Node *right; 5 }; 6 7 bool CompareTree(Node *first, Node *second) { 8 if(first==NULL && second==NULL) 9 return true; 10 if((first==NULL && second!=NULL) || 11 (first!=NULL && second==NULL)) 12 return false; 13 if(first->value!=second->value) 14 return false; 15 return CompareTree(first->left,second->left) && 16 CompareTree(first->right, second->right); 17 }
笔试算法题(27):判断单向链表是否有环并找出环入口节点 & 判断两棵二元树是否相等,布布扣,bubuko.com
笔试算法题(27):判断单向链表是否有环并找出环入口节点 & 判断两棵二元树是否相等
标签:style c class blog code java
原文地址:http://www.cnblogs.com/leo-chen-2014/p/3747122.html