求二叉树中节点的最大距离

二叉树

一、题目: (感谢  http://blog.csdn.net/v_JULY_v  提供的题目)

求二叉树中节点的最大距离

如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们定义"距离"为两节点之间边的个数。
写一个程序,求一棵二叉树中相距最远的两个节点之间的距离。

二、分析:
一个数的距离有两种:1.树根到树叶的距离;2.树叶和树叶的距离。即,

采用递归的方式计算出二叉树中最长的距离。

三、代码:
#include<iostream>
using namespace std;

struct result		//用于保存寻找最大距离时返回的结果
{
	int deep;		//深度
	int distance;	//子树结点总和
};

struct BSTreeNode	//树节点
{
	int data;
	BSTreeNode *left;
	BSTreeNode *right;
};

class BSTree
{
public:
	BSTree(){this->root = Create();maxLength = 0;}
	~BSTree(){Release(this->root);}
	BSTreeNode *GetRoot();
	void InOrder(BSTreeNode *root);		//中序遍历
	void getMaxDistance(BSTreeNode *root);
private:
	BSTreeNode *root;
	BSTreeNode *Create();
	void Release(BSTreeNode *root);
	int maxLength;		//记录最大距离(节点个数)
	result getMaxLength(BSTreeNode *root);
};

void BSTree::Release(BSTreeNode *root)  
{  
      if (root != NULL){                    
		  Release(root->left);   //释放左子树  
		  Release(root->right);   //释放右子树  
          delete root;  
      }  
}  

void BSTree::getMaxDistance(BSTreeNode *root)
{
	this->getMaxLength(root);
	cout<<"MaxDistance is : "<<this->maxLength - 1;//结点距离 = 节点个数 - 1
}

result BSTree::getMaxLength(BSTreeNode *root)
{
	result res;
	if (root==NULL)  
	{
		res.deep = 0;
		res.distance = 0;
	}
	else{
		result L_res = getMaxLength(root->left);    //中序递归遍历root的左子树
		result R_res = getMaxLength(root->right);    //中序递归遍历root的右子树

		res.deep = L_res.deep > R_res.deep ? L_res.deep+1 : R_res.deep+1;
		res.distance = L_res.deep + R_res.deep + 1;
	}
	maxLength = maxLength > (res.deep > res.distance ? res.deep : res.distance) 
                       ? maxLength : (res.deep > res.distance 
                                               ? res.deep : res.distance);
	return res; 
}

void BSTree::InOrder(BSTreeNode *root)
{
	if (root==NULL)  return;      //递归调用的结束条件	          
	else{	
		InOrder(root->left);    //中序递归遍历root的左子树
		cout<<root->data<<" ";    //访问根结点的数据域
		InOrder(root->right);    //中序递归遍历root的右子树
	}
}

BSTreeNode *BSTree::GetRoot()
{
	return this->root;
}

BSTreeNode *BSTree::Create()
{
	BSTreeNode *root;
	int ch;
	cout<<"请输入节点数据:"<<endl;
	cin>>ch;
	if(ch==0) root = NULL;
	else{
		root = new BSTreeNode;
		root->data = ch;
		root->left = Create();
		root->right = Create();
	}
	return root;
}

void main()
{
	BSTree bst;
	BSTreeNode *root = bst.GetRoot();
	bst.InOrder(root);
	cout<<endl;
	bst.getMaxDistance(root);
}

思路:

result BSTree::getMaxLength(BSTreeNode *root)
{
	result res;
	if (root==NULL)  
	{
		res.deep = 0;
		res.distance = 0;
	}
	else{
		result L_res = getMaxLength(root->left);    //中序递归遍历root的左子树
		result R_res = getMaxLength(root->right);    //中序递归遍历root的右子树

		res.deep = L_res.deep > R_res.deep ? L_res.deep+1 : R_res.deep+1;
		res.distance = L_res.deep + R_res.deep + 1;
	}
	maxLength = maxLength > (res.deep > res.distance ? res.deep : res.distance) 
                       ? maxLength : (res.deep > res.distance 
                                               ? res.deep : res.distance);
	return res; 
}


1. 叶子:     返回  深度           结点个数:1
                               子树距离   结点个数:1


2.非叶子:  返回  深度            结点个数: max(左深,右深)+ 1
                               子树距离    结点个数: 左深+右深 + 1




**输入数据格式为(1为有节点,0为无节点):

(测试1)1 ENTER 1 ENTER 1 ENTER 0 ENTER 0 ENTER 1 ENTER 0 ENTER 0 ENTER 1 ENTER 0 ENTER 0 ENTER


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值