程序员面试题精选100题(48)-二叉树两结点的最低共同父结点
2013-01-03 22:06
615 查看
// 程序员面试题精选100题(48)-二叉树两结点的最低共同父结点.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <list> using namespace std; #define N 7 struct TNode{ char chValue; TNode *leftChild; TNode *rightChild; }; struct ListNode {//used for calculating the lowest common root,two lists record the search routes of the two point to be searched char chValue; ListNode* next; }; TNode* rebuildTree(char *preOrder,int start1,char *midOrder,int start2,int n)//根据前序和中序建立二叉树 长度为N { TNode *root; if(n==0) return NULL; root = new TNode; int pivet; char chroot; chroot=preOrder[start1]; root->chValue = chroot; for(int i=start2;i<start2+n;i++)//include start1 { if(midOrder[i]==chroot) { pivet = i;break;} } if(pivet-start2>0) root->leftChild=rebuildTree(preOrder,start1+1,midOrder,start2,pivet-start2); else root->leftChild = NULL; if(n-pivet+start2-1>0)//pivet-start2 is the half number, root->rightChild=rebuildTree(preOrder,start1+1+pivet-start2,midOrder,pivet+1,n-pivet+start2-1); else root->rightChild = NULL; return root; } void printList(ListNode* head) { ListNode* temp=head; cout<<" list is "; while(temp!=NULL){ cout<<temp->chValue<<" "; temp=temp->next; } cout<<endl; } bool SearchWithList(TNode* root,char val,ListNode* head)// record the searching route in the list { if (root==NULL)//does not find { return false; } if (root->chValue==val) { /*ListNode *temp=new ListNode;//here, if this is none, then the same as the answer temp->chValue = root->chValue; temp->next=NULL; head->next=temp;*/ return true; } else { ListNode *temp1=new ListNode; temp1->chValue = root->chValue; temp1->next=NULL; head->next=temp1; if (!SearchWithList(root->leftChild,val,temp1))// if there no exist in the left node, then turn to the right node,otherwise, stop { ListNode *temp2=new ListNode; temp2->chValue = root->chValue; temp2->next=NULL; head->next=temp2;//change head->next, previous become invalidate if (!SearchWithList(root->rightChild,val,temp2)) { head->next=NULL; return false; } else return true; } else return true; } } char LowestCommonRoot(TNode* root,char ch1,char ch2) { ListNode *head1,*head2; head1= new ListNode; head1->chValue = '#';// head value is space head1->next=NULL; head2= new ListNode; head2->chValue = '#'; head2->next=NULL; bool bl1=SearchWithList(root,ch1,head1); bool bl2=SearchWithList(root,ch2,head2); if (bl1) { printList(head1); } if (bl2) { printList(head2); } if (!bl1&&!bl2) { return '#';//means does not exist } else { ListNode *p1=head1,*p2=head2; if (p1->chValue!=p2->chValue) { return '#'; } while(p1->next!=NULL&&p2->next!=NULL&&p1->next->chValue==p2->next->chValue) { p1=p1->next;p2=p2->next; } return p1->chValue; } } ////////////////////////////////////////////////////////////////////////// bool GetNodePath(TNode* pHead, TNode* pNode, std::list<TNode*>& path) { if(pHead == pNode) return true; path.push_back(pHead); bool found = false; if(pHead->leftChild != NULL) found = GetNodePath(pHead->leftChild, pNode, path); if(!found && pHead->rightChild) found = GetNodePath(pHead->rightChild, pNode, path); if(!found) path.pop_back(); return found; } TNode* LastCommonNode(const std::list<TNode*>& path1,const std::list<TNode*>& path2) { std::list<TNode*>::const_iterator iterator1 = path1.begin(); std::list<TNode*>::const_iterator iterator2 = path2.begin(); TNode* pLast = NULL; while(iterator1 != path1.end() && iterator2 != path2.end()) { if(*iterator1 == *iterator2) pLast = *iterator1; iterator1++; iterator2++; } return pLast; } TNode* LastCommonParent_2(TNode* pHead, TNode* pNode1, TNode* pNode2) { if(pHead == NULL || pNode1 == NULL || pNode2 == NULL) return NULL; std::list<TNode*> path1; GetNodePath(pHead, pNode1, path1); std::list<TNode*> path2; GetNodePath(pHead, pNode2, path2); return LastCommonNode(path1, path2); } ////////////////////////////////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { char preOrder ={'A','B','C','D','E','F','G'},midOrder ={'C','B','D','A','F','E','G'}; /*cout<<"input preorder"<<endl; for(int i=0;i<N;i++) cin>>preOrder[i]; cout<<"input midorder"<<endl; for(int i=0;i<N;i++) cin>>midOrder[i];*/ TNode *root; root=rebuildTree(preOrder,0,midOrder,0,N); cout<<endl<<" root value is "<<root->chValue<<endl; cout<<endl<<"common root is "<<LowestCommonRoot(root,'F','E')<<endl; TNode *lnode,*rnode; //lnode=root->leftChild; // result is A? right //rnode=root->leftChild->rightChild; lnode=root->rightChild;//->leftChild; rnode=root->rightChild->rightChild; cout<<lnode->chValue<<endl; cout<<rnode->chValue<<endl; cout<<" the second method value is "<<LastCommonParent_2(root,lnode,rnode)->chValue<<endl; system("pause"); return 0; }what are the differences between the stardard answer and yours? there are many aspects to learn.
相关文章推荐
- 程序员面试题精选100题(48)-二叉树两个结点的最低共同父结点
- 程序员面试题精选100题(48)-二叉树两结点的最低共同父结点
- 程序员面试题精选100题(48)-二叉树两个结点的最低共同父结点
- 程序员面试题精选100题(48)-二叉树两结点的最低共同父结点[数据结构]
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
- 程序员面试100题之五:二叉树两个结点的最低共同父结点
- 程序员面试100题之五:二叉树两个结点的最低共同父结点
- 程序员面试100题之五:二叉树两个结点的最低共同父结点
- 程序员面试100题之五:二叉树两个结点的最低共同父结点
- [程序员面试题精选100题]4.二叉树中和为某一值的所有路径
- 二叉树两个结点的最低共同父结点
- 二叉树两结点的最低共同父结点
- 二叉树两个结点的最低共同父结点
- 二叉树两个结点的最低共同父结点
- 程序员面试题精选100题(09)-查找链表中倒数第k个结点
- 程序员面试题精选100题(33)-在O(1)时间删除链表结点
- 程序员面试题精选100题(60)-判断二叉树是不是平衡
- 【编程题目】二叉树两个结点的最低共同父结点
- 二叉树两个结点的最低共同父结点
- 二叉树两个结点的最低共同父结点(树)