各大计算机公司 笔试及面试 题目 - 腾讯、网易、淘宝(求多叉树及二叉树中最远叶子节点的距离)

#include<iostream>
#include<stack>
#include<queue>
#include<cstdlib>
#include<ctime>


using namespace std;

/***********************************************************
*
*普通的树(多叉) 腾讯面试题-求最远的两个叶子节点间的距离
*
************************************************************/

typedef struct node
{
	int data;
	int child_num;
	node** childs;
}*tree;

int maxDiameter=0;

int Diameter(tree t)
{
	if(t->child_num==0)
		return 1;

	tree ptr=NULL;
	int max1=0,max2=0,tmp=0;
	for(int i=0;i<t->child_num;i++)
	{
		ptr=t->childs[i];

		if(ptr !=NULL)
		{
			tmp=Diameter(ptr);

			if(max1>max2 && tmp>max2)
				max2=tmp;
			else if(max2>=max1 && tmp>max1)
				max1=tmp;
				
		}
	}
	if(max1+max2>maxDiameter)
	{
		maxDiameter=max1+max2;
	}

	return (max1>max2?max1+1:max2+1);	
}

//创建多叉树
void  createTNode(tree* tnode)
{
	int tmp=0;
	cin>>tmp;

	if(tmp>0)
	{
		(*tnode) = (tree)malloc(sizeof(**tnode));

		(*tnode)->data=tmp;
		cout<<tmp<<"'s count of son\n";
		int icnt=0;
		cin>>icnt;
		
		(*tnode)->child_num=icnt;
		(*tnode)->childs=(tree*)malloc(icnt*sizeof(tree));

		for(int i=0;i<icnt;i++)
		{
			cout<<tmp<<"'s "<<i<<"st son\n";
			createTNode(&((*tnode)->childs[i]));
		}

	}
	else *tnode=NULL;
 
}


void DFSTraverse(tree node)
{
	cout<<"\n----------Depth First Traverse--------------\n";

	stack<tree> stk;
	if(node)
		stk.push(node);
	else
		return ;
	while(!stk.empty())
	{
		node=stk.top();
		stk.pop();
		cout<<node->data<<"\t";

		for(int i=node->child_num-1;i>=0;i--)
		{
			stk.push(node->childs[i]);
		}
	}
}

void BFSTraverse(tree node)
{
	cout<<"\n----------Broad First Traverse--------------\n";

	queue<tree> que;
	if(node)
		que.push(node);
	else
		return ;

	while(!que.empty())
	{
		node=que.front();
		que.pop();
		cout<<node->data<<"\t";

		for(int i=0;i<node->child_num;i++)
		{
			que.push(node->childs[i]);
		}
	}

}
/***********************************************************
*
*二叉树 淘宝和网易的面试题
*
************************************************************/
typedef struct tnode
{
	int data;
	struct tnode* left;
	struct tnode* right;
} TreeNode;

int getLongestDist(TreeNode * node,int& maxDist)
{
	if(node->left ==NULL && node->right ==NULL)
		return 1;

	int leftlen=0,rightlen=0;

	if(node->left !=NULL)
	{
		leftlen=getLongestDist(node->left,maxDist);
		cout<<node->data<<"'s leftlen ="<<leftlen<<" maxDist="<<maxDist<<endl;
	}
 
	if(node->right !=NULL)
	{
		rightlen=getLongestDist(node->right,maxDist);
		cout<<node->data<<"'s rightlen ="<<rightlen<<" maxDist="<<maxDist<<endl;
	}

	if(leftlen+rightlen>maxDist)
	{
		maxDist=leftlen+rightlen;
		cout<<"maxdist="<<maxDist<<endl;
	}

	return (leftlen>rightlen?leftlen+1:rightlen+1);
}

//创建二叉树
void  createBiTNode(TreeNode** tnode)
{
	//srand(clock());
	int tmp=0;
	cin>>tmp;

	if(tmp>0)
	{
		*tnode = new TreeNode;
		//(*tnode)->data=rand();
		//cin>>(*tnode)->data;
		//cout<<"n="<<n<<endl;
		(*tnode)->data=tmp;
		cout<<tmp<<"'s left\n";
		createBiTNode(&((*tnode)->left));
		cout<<tmp<<"'s right\n";
		createBiTNode(&((*tnode)->right));
	}
	else *tnode=NULL;
 
}

void  preTraverse(TreeNode *tnode)
{
	cout<<"\n----------Pre Order Traverse--------------\n";

	stack<TreeNode *> stk;

	while(tnode || !stk.empty())
	{
		if(tnode)
		{
			cout<<tnode->data<<"\t";
			stk.push(tnode);
			tnode=tnode->left;
		}
		else
		{
			tnode=stk.top();
			stk.pop();
			tnode=tnode->right;
		}
		
	}
	cout<<"\n";
}

void midTraverse(TreeNode* tnode)
{
	cout<<"\n----------In Order Traverse--------------\n";
	stack<TreeNode*> stk;
	while(tnode || !stk.empty())
	{
		if(tnode)
		{
			stk.push(tnode);
			tnode=tnode->left;
		}
		else
		{
			tnode=stk.top();
			cout<<tnode->data<<"\t";
			
			tnode=tnode->right;
			stk.pop();
		}
	}
	cout<<"\n";
}

int main(int argc,char** argv)
{
	//普通树
	{
		tree root=NULL;
		createTNode(&root);

		DFSTraverse(root);
		
		BFSTraverse(root);

		Diameter(root);

		cout<<"\nmaxDiameter="<<maxDiameter<<endl;

	}

	//二叉树
	{
		TreeNode * root=NULL;
		createBiTNode(&root);
	
		preTraverse(root);

		midTraverse(root);
	
		int maxdist=0;
		getLongestDist(root,maxdist);

		cout<<"\nmaxdist="<<maxdist<<endl;
	}
	
	system("pause");
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值