程序员面试题精选100题(48)-二叉树两结点的最低共同父结点

// 程序员面试题精选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[N]={'A','B','C','D','E','F','G'},midOrder[N]={'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. 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值