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

class BTree
{
public:
struct Node
{
int _val;
Node * _lChild;
Node * _rChild;
Node * _pParent;
};
int MaxLength();
int MaxLength(BTree::Node * node, int & maxLen);
}

int BTree::MaxLength()
{
int maxLen =0;
MaxLength(_pRoot,maxLen);
return maxLen;
}

int BTree::MaxLength(BTree::Node * node, int & maxLen)
{
if(node==0)
return 0;
int l = Height(node->_lChild);
int r = Height(node->_rChild);
if(maxLen<(l+r))
maxLen = l+r;
return 1 + max(l,r);
}

 

posted on 2011-11-04 18:05  SammyLan  阅读(277)  评论(0编辑  收藏  举报

导航