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;
}
}
查找二叉树最远两个节点的距离
原文作者:二叉查找树
原文地址: https://blog.csdn.net/my_acm/article/details/46664877
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/my_acm/article/details/46664877
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。