您的位置:首页 > 其它

查找二叉树最远两个节点的距离

2015-06-27 22:47 363 查看
struct Node{
Node * left;
Node * right;
int lmaxlen;
int rmaxlen;
char val;
};

int maxlen = 0;

//查找二叉树中最远的两个节点
void search(Node * root){
if(root == NULL){
return ;
}
if(root->left != NULL){
search(root->left);
}else{
root->lmaxlen = 0;
}
if(root->right != NULL){
search(root->right);
}else{
root->rmaxnlen = 0;
}
if(root->left != NULL){
int tpmaxlen = 0;
if(root->left->lmaxlen > root->left->rmaxlen){
tpmaxlen = root->left->lmaxlen;
}else{
tpmaxlen = root->left->raxlen;
}
root->lmaxlen = tpmaxlen + 1;
}
if(root->right != NULL{
int tpmaxlen = 0;
if(root->right->rmaxlen > root->right->lmaxlen){
tpmaxlen = root->right->rmaxlen;
}else{
tpmaxlen = root->right->lmaxlen;
}
root->lmaxlen = tpmaxlen + 1;
}
if(root->lmaxlen + root->rmaxlen > maxlen){
maxlen = root->lmaxlen + root->rmaxlen;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: